2009-03-13 29 views
31

Tôi muốn sử dụng kiểu dáng hộp văn bản Aero, nhưng vẫn ghi đè lên một số thuộc tính. Tôi cố gắng để thực hiện của thành viên này:Ghi đè kiểu dáng mặc định trong Hộp văn bản WPF, dựa trên PresentationFramework.Aero

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" /> 
    </ResourceDictionary.MergedDictionaries> 

    <Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}"> 
     <Setter Property="Margin" Value="2" /> 
     <Setter Property="Padding" Value="2" /> 
    </Style> 
</ResourceDictionary> 

Tuy nhiên, kết quả này trong một StackOverflowException khi khởi động ứng dụng của tôi. Khi tôi loại bỏ các tham chiếu đến PresentationFramework.Aero, điều này làm việc nhưng tôi nhận được phong cách hệ điều hành mặc định, mà làm cho các ứng dụng xấu xí. ;)

Vì vậy, có hiệu lực: nếu tôi muốn ghi đè một số kiểu trên tất cả các hộp văn bản của mình, tôi không thể có giao diện Aero. Nếu tôi muốn giao diện Aero, tôi không thể ghi đè lên bất kỳ kiểu dáng nào. Deadlock.

Bất kỳ cách nào để giải quyết vấn đề này?

+0

Trong bình luận của bạn để Roberts câu trả lời bên dưới, bạn dường như gợi ý mà bạn đã làm việc này với resourcedictionaries cấp cao nhất. Hãy chia sẻ những gì bạn nghĩ ra. – Oskar

Trả lời

35

Có vẻ như để làm việc nếu bạn đặt Style như một nguồn lực cấp dưới, thay vì trong ResourceDictionary cùng:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Grid.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Grid.Resources> 
    <Border BorderBrush="Blue" BorderThickness="3"> 
     <Border.Resources> 
      <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}"> 
       <Setter Property="Margin" Value="2" /> 
       <Setter Property="Padding" Value="2" /> 
      </Style> 
     </Border.Resources> 
     <TextBox /> 
    </Border> 
</Grid> 
+0

Tuyệt vời, hoạt động. Tôi đặt các phong cách trong một ResourceDictionary khác ở cùng một mức độ của từ điển bên ngoài, và StackOverflow biến mất. Cảm ơn! – Inferis

+5

Cảm ơn câu trả lời này, nhưng có anyway để có được điều này để làm việc với cả aero resourcedictionary và phong cách riêng của tôi được định nghĩa trong Application.Resources (App.xaml)? Tôi thực sự không thể đủ khả năng để đặt này trên ví dụ như mỗi cửa sổ .. – Oskar

12

Không giống như các mã trong câu trả lời chấp nhận này cho phép sử dụng từ điển tài nguyên cho phong cách. Shamelessly bị đánh cắp từ http://social.msdn.microsoft.com/forums/en-US/wpf/thread/3c66adb7-fd26-40c7-8404-85f6fefbd392/ trả lời bằng cách Vivien Ruitz

<!--App.xaml--> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/MyAppli;component/Resources/Themes/StyleDictionary.xaml"/> 
      <ResourceDictionary Source="/MyAppli;component/Resources/Themes/ApplyStyleDictionary.xaml"/> 
      ... 
     </ResourceDictionary.MergedDictionaries> 

<!--StyleDictionary.xaml--> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/aero.normalcolor.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
     <Style x:Key="ButtonStyleToApply" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}" > 
      ... <!--Extend the aero style here--> 
     </Style> 

<!--ApplyStyleDictionary.xaml--> 
     <Style TargetType="Button" BasedOn="{StaticResource ButtonStyleToApply}"/> 
Các vấn đề liên quan