WPF,如何覆盖特定位置的文本块上的应用程序范围样式
我有一个样式 xaml 资源字典,它添加到 Application.xaml 中。 在该样式文件中,我指定所有文本块的前景应为白色。 问题是,这会将同一应用程序中的用户控件中的组合框项目前景更改为白色。我希望这些项目在所有或仅在这一个组合框中具有黑色前景。 我很难做到这一点。
这是我的文本块的全局样式:
<Style TargetType="{x:Type TextBlock}" >
<Setter Property="Foreground">
<Setter.Value>
White
</Setter.Value>
</Setter>
<Setter Property="Height">
<Setter.Value>
23
</Setter.Value>
</Setter>
</Style>
另外:用户控件在代码隐藏中动态添加组合框。
这可以做到吗?如何?
我根据雷·伯恩斯的评论进行了更改。 这是我的 MyCustomStyler:
Public Class MyCustomStyler
Inherits DependencyObject
Public Shared Function GetStyle1(ByVal obj As DependencyObject) As Style
Return obj.GetValue(Style1Property)
End Function
Public Shared Sub SetStyle1(ByVal obj As DependencyObject, ByVal value As Style)
obj.SetValue(Style1Property, value)
End Sub
Public Shared instancePropertyChangedCallback As New PropertyChangedCallback(AddressOf PropertyChangedCallback_Handler)
Public Shared ReadOnly Style1Property As DependencyProperty = _
DependencyProperty.RegisterAttached("Style1", _
GetType(Style), GetType(MyCustomStyler), _
New FrameworkPropertyMetadata(instancePropertyChangedCallback))
Public Shared Sub PropertyChangedCallback_Handler(ByVal obj As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim element = CType(obj, FrameworkElement)
Dim style = CType(e.NewValue, Style)
element.Resources(style.TargetType) = style
End Sub
End Class
这是我的样式部分:
<Style TargetType="ComboBox">
<Setter Property="local:MyCustomStyler.Style1">
<Setter.Value>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Black" />
</Style>
</Setter.Value>
</Setter>
</Style>
虽然无法让它工作..仍然是白色前景...
I have a style xaml resource dictionary which is added in Application.xaml.
In that style file I specify that all textblocks should have the foreground white.
The problem is that this will change the combobox items foreground to white in a usercontrol I have in the same application. I want the items to have a black foreground in all or only this one combobox.
I'm having big troubles making that happen.
This is my global style for textblocks:
<Style TargetType="{x:Type TextBlock}" >
<Setter Property="Foreground">
<Setter.Value>
White
</Setter.Value>
</Setter>
<Setter Property="Height">
<Setter.Value>
23
</Setter.Value>
</Setter>
</Style>
Also: The usercontrol adds the combobox dynamically in the code-behind.
Can this be done? How?
I've made changes according to Ray Burns comment.
This is my MyCustomStyler:
Public Class MyCustomStyler
Inherits DependencyObject
Public Shared Function GetStyle1(ByVal obj As DependencyObject) As Style
Return obj.GetValue(Style1Property)
End Function
Public Shared Sub SetStyle1(ByVal obj As DependencyObject, ByVal value As Style)
obj.SetValue(Style1Property, value)
End Sub
Public Shared instancePropertyChangedCallback As New PropertyChangedCallback(AddressOf PropertyChangedCallback_Handler)
Public Shared ReadOnly Style1Property As DependencyProperty = _
DependencyProperty.RegisterAttached("Style1", _
GetType(Style), GetType(MyCustomStyler), _
New FrameworkPropertyMetadata(instancePropertyChangedCallback))
Public Shared Sub PropertyChangedCallback_Handler(ByVal obj As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim element = CType(obj, FrameworkElement)
Dim style = CType(e.NewValue, Style)
element.Resources(style.TargetType) = style
End Sub
End Class
This is my style section:
<Style TargetType="ComboBox">
<Setter Property="local:MyCustomStyler.Style1">
<Setter.Value>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Black" />
</Style>
</Setter.Value>
</Setter>
</Style>
Can't get it to work though.. Still white foreground...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看待这个问题的方法是:如何使 ComboBox 使用与 UI 其余部分不同的 TextBox 样式?
答案是:创建一个应用其他样式的附加属性,然后使用 ComboBox 样式来应用您的附加属性。
首先是 ComboBox 样式,这样您将看到它的走向:
现在要使其工作,您需要定义 MyCustomStyler 类,该类将如下所示:
其工作方式是,每次设置附加的 Style1 属性时在 FrameworkElement(例如 ComboBox)上,它将样式添加到默认键下的资源中。因此,每当将上述样式应用于 ComboBox 时,内部 TextBox 样式就会添加到 ComboBox 的资源中,从而使 TextBox 获得此样式。
从这里开始,实现您想要的内容很简单:只需将 ComboBox 样式与自定义 TextBox 样式一起放入 App.xaml 中即可。
The way to look at this question is: How can I make ComboBoxes use a different TextBox style than the rest of my UI?
The answer is: Create an attached property that applies the other style, then use a ComboBox style to apply your attached property.
First the ComboBox style so you'll see where this is headed:
Now for this to work you'll need to define the MyCustomStyler class, which will be something like this:
The way this works is, every time you set the attached Style1 property on a FrameworkElement (such as a ComboBox), it adds the style to the Resources under the default key. So whenever the above-shown Style is applied to a ComboBox, the internal TextBox style will be added to the ComboBox's resources, causing the TextBox to get this style.
From here it is simple to achieve what you are looking for: Just put the ComboBox style in your App.xaml alongside your custom TextBox style and you're done.