无法使用 className.property 设置样式
我是 wpf 新手,我想使用样式和 className.property
该样式不适用于第一个文本框
,因为事实上它适用于堆栈面板
我是否遗漏了什么?
<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>
</StackPanel>
</Grid>
请查看这篇文章 在其中找到 Property="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>
</StackPanel>
</Grid>
take a look at this article
find Property="ClassName.Property" in that
thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想为堆栈面板中的所有文本框设置样式,请尝试这个
您可以使用
TargetType="{x:Type TextBox}
指定它If you want to the Set style for all TextBoxes in a stack panel then try this one
You can specify it by using the
TargetType="{x:Type TextBox}
这里是另一个例子,可以更好地理解 setter 的工作原理:
这里,TextBox.Height 并不是指 TextBox 的高度,而只是指向一个名为 Height 的依赖属性。此行为是由于该样式没有 TargetType。
因此,在此示例中,文本框的高度将保留为默认值,只有 stackpanel 的高度将更改为 100。
在 FontFamily 的示例中也会发生类似的情况。实际发生的情况是,setter 将 StackPanel FontFamiliy 属性设置为 setter 中的值。另一件需要记住的重要事情是,某些属性是在父控件与其子控件之间继承的。
但您需要小心,因为属性不会被继承。例如 FontFamily 是可以的,但是 Foreground 不能被 TextBox 控件继承。
我认为您可以使用类似内容的唯一方法
是当您要应用该样式的控件的可视化树包含 ClassName 类型的元素时。
例如
,这里 StackPanel 在其可视化树中包含一个 TextBlock,因此现在将对其应用设置器。
此外,如果您在样式中定义 TargetType,它将知道在哪些控件上应用模板。
此处是一个描述控件可视树的链接。
So here is another example to better understand how the setter works:
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
is when the visual tree of the control on which you want to apply that style contains an element of type ClassName.
For example
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.