开发者

Cant set the style using className.property

Im new to wpf i want to use a style and className.property

the style does not apply to first text box

as the mater of fact it applies to stack panel

Am i missing something?

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="883">
<Grid>
    <Grid.Resources >
        <Style x:Key="m">
            <Setter Property="TextBox.Background" Value="Aqua"/>
        </Style>
    </Grid.Resources>
    <TextBlock >adsdfsadfasdfad</TextBlock>
    <StackPanel Orientation="Horizontal" Style="{StaticResource m}">
        <TextBox HorizontalAlignment="Stretch" Width="193"  Margin="50">
        </TextBox>
        <TextBox HorizontalAlignment="Stretch" Width="193" Background="Black" Margin="50">
        </TextBox>
    </Sta开发者_如何学GockPanel>
</Grid>

take a look at this article find Property="ClassName.Property" in that thank you.


If you want to the Set style for all TextBoxes in a stack panel then try this one

<Grid>
    <Grid.Resources >
         <Style x:Key="m" TargetType="{x:Type TextBox}">
         <Setter Property="Background" Value="Blue" />
       </Style>
   </Grid.Resources>
 <TextBlock >adsdfsadfasdfad</TextBlock>
      <StackPanel Orientation="Horizontal" >
          <StackPanel.Resources>
               <Style BasedOn="{StaticResource m}" TargetType="{x:Type TextBox}" />
          </StackPanel.Resources>
          <TextBox HorizontalAlignment="Stretch" Name="ss" Width="193" Margin="50"/>
         <TextBox Width="193"  Margin="50"/>
     </StackPanel>
</Grid>

You can specify it by using the TargetType="{x:Type TextBox}


So here is another example to better understand how the setter works:

<Window.Resources>
    <Style x:Key="m">
        <Setter Property="TextBox.Height"
                Value="100" />
    </Style>
</Window.Resources>
<Grid>
    <StackPanel Style="{StaticResource m}">
        <TextBlock>My Sample</TextBlock>
        <TextBox>My text box</TextBox>
    </StackPanel>
</Grid>

Here, the TextBox.Height is not referring to the height of the TextBox, but is simply pointing to a dependency property called Height. This behavior is due to the fact that the style does not have a TargetType. So in this sample, the height of the text box will remain as default, and only the height of the stackpanel will change to 100.

Something like that happens also in the example with FontFamily. What actually happens is that the setter sets the StackPanel FontFamiliy property to the value in the setter. Another important thing to keep in mind is that some properties are inherited between the parent control and their children. But you need to be careful as not properties are inherited. For example FontFamily is ok, but Foreground is not inherited by TextBox control.

I think the the only way you can use something like

Property = "ClassName.Property" 

is when the visual tree of the control on which you want to apply that style contains an element of type ClassName.

For example

 <Window.Resources>
    <Style x:Key="m">
        <Setter Property="TextBlock.FontFamily"
                Value="Aharoni" />
    </Style>
 </Window.Resources>
 <Grid>
    <StackPanel Style="{StaticResource m}">
        <TextBlock>My Sample</TextBlock>
    </StackPanel>
 </Grid>

Here the StackPanel contains in it's visual tree a TextBlock, and consequently is will now to apply the setter to it.

Further more, if you define a TargetType inside the style, it will know on which controls to apply the template.

Here is a link that describes a little what is the visual tree of a control.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜