2011-09-29 12 views
5

Veri giriş formunu oluşturmak için aşağıdakine benzer bir şey yazmam benim için nadir değildir, ancak benim sorunum, ve TextBlock'un BaseElementStyle'daki Ayarlayıcıları uygulamamasıdır. Genellikle bunları ayrı olarak tanımlamam gerekir. Neden buTextBlock/TextBox'ım neden bir Temel Stilden değerleri uygulamıyor?

mı? Ve bunun etrafında bir yolu var mı?

Ben genellikle diğer kontrol şablonları kullanılır aslında (örnek TextBlock en kontrollerde kullanılır ve TextBox datepickers kullanılır ve Comboboxes)

<Style x:Key="BaseElementStyle" TargetType="{x:Type FrameworkElement}"> 
    <Setter Property="Margin" Value="5" /> 
    <Setter Property="VerticalAlignment" Value="Center" /> 
</Style> 
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource BaseElementStyle}" /> 
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseElementStyle}" /> 
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource BaseElementStyle}" /> 
<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource BaseElementStyle}" /> 
<Style TargetType="{x:Type DatePicker}" BasedOn="{StaticResource BaseElementStyle}" /> 
<Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseElementStyle}" /> 

cevap

8

isterim ilgisi var tahmin ediyorum iki olası geçici çözümü önermek için. Anahtar ve Tip'in her birinin kullanılabileceği görülüyor, ancak her ikisi de soru durumunuz olarak birlikte kullanılamaz, x:Key="BaseElementStyle" TargetType="{x:Type FrameworkElement}".

  1. kullanarak x: x kullanarak

    <Style x:Key="BaseElementStyle"> 
        <Setter Property="FrameworkElement.Margin" Value="5" /> 
        <Setter Property="FrameworkElement.VerticalAlignment" Value="Center" /> 
    </Style> 
    <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource BaseElementStyle}" /> 
    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseElementStyle}" /> 
    <Style TargetType="{x:Type Label}" BasedOn="{StaticResource BaseElementStyle}" /> 
    <Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource BaseElementStyle}" /> 
    <Style TargetType="{x:Type DatePicker}" BasedOn="{StaticResource BaseElementStyle}" /> 
    <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseElementStyle}" /> 
    
  2. Anahtar: Ayrıca WPF bir ControlTemplate gördüğü akılda tutulması Tipi

    <Style TargetType="{x:Type FrameworkElement}"> 
        <Setter Property="Margin" Value="5" /> 
        <Setter Property="VerticalAlignment" Value="Center" /> 
    </Style> 
    <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type FrameworkElement}}" /> 
    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type FrameworkElement}}" /> 
    <Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type FrameworkElement}}" /> 
    <Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type FrameworkElement}}" /> 
    <Style TargetType="{x:Type DatePicker}" BasedOn="{StaticResource {x:Type FrameworkElement}}" /> 
    <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource {x:Type FrameworkElement}}" /> 
    
+0

teşekkür ederiz! Ve bana yeni bir şey öğrettin ... # 2'de gösterdiğiniz gibi bir stili üslupta tutabileceğinin farkında değildim. Bilmek çok güzel! – Rachel

7

enflasyon sınır olması ve yapar etmek NOT, şablonların içinde varsayılan stiller uygular. Kuralın istisnası: Controlöğesinden devralan her şey varsayılan stille şişirilmiş OLACAKTIR. TextBlock, FrameworkElement'dan itibaren ve Control'ten değil, ControlTemplate'un içinden kullanırsanız, stili manuel olarak uygulamanız gerekir. Bu, el ile eklenen TextBlocks veya Content dizesi için WPF tarafından eklenen TextBlocks için geçerlidir. Hızlı bir örnek:

<Window x:Class="ImplicitStyles.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <StackPanel> 
     <StackPanel.Resources> 
      <Style x:Key="BaseElementStyle"> 
       <Setter Property="FrameworkElement.Tag" Value="Hello World" /> 
      </Style> 
      <Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseElementStyle}" /> 
      <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource BaseElementStyle}" /> 

      <!-- Default style for TextBlock will not be applied, Tag will be null --> 
      <ControlTemplate x:Key="MyContentControlTemplateOne" TargetType="{x:Type ContentControl}"> 
       <Border BorderBrush="Red" BorderThickness="2"> 
        <TextBlock Text="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" /> 
       </Border> 
      </ControlTemplate> 

      <!-- Default style for Button will be applied, Tag will be Hello World -->  
      <ControlTemplate x:Key="MyContentControlTemplateTwo" TargetType="{x:Type ContentControl}"> 
       <Border BorderBrush="Red" BorderThickness="2"> 
        <Button Content="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" /> 
       </Border> 
      </ControlTemplate> 

     </StackPanel.Resources> 

     <ContentControl Template="{StaticResource MyContentControlTemplateOne}" /> 
     <ContentControl Template="{StaticResource MyContentControlTemplateTwo}" /> 
    </StackPanel> 

</Window> 

fazla bilgi için, bu blog yazısı bkz:

http://blogs.msdn.com/b/wpfsdk/archive/2009/08/27/implicit-styles-templates-controls-and-frameworkelements.aspx

+1

Teşekkürler, Şablonların stil kalıtım sınırları olduğunu fark etmedim. DataTemplates içindeki TextBlocks'un neden aynı stilde uygulanmadığını merak ediyordum – Rachel

İlgili konular