Window 样式资源不覆盖 UserControl 属性 出现时
Window Style Resource Does Not Override UserControl Property When Present
鉴于我有一个来自某个名为 SomeControl
的控件
在 MyUserControl.xaml 中,我这样使用 SomeControl
:
<Grid.Resources>
<Window.Resources>
<Style TargetType="local:SomeControl">
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip>
<TextBlock Text="FOO"/>
</ToolTip>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
</Grid.Resources>
<Grid>
<!-- Others controls in here -->
<local:SomeControl />
</Grid>
在Window.xaml中:
<Window.Resources>
<Style TargetType="local:SomeControl">
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip>
<TextBlock Text="BAR"/>
</ToolTip>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<local:MyUserControl />
</Grid>
结果是我要它显示Bar
时它会显示Foo
。
如果我从 UserControl 中删除工具提示,将使用 Window 的样式,它会像我期望的那样显示 Bar
。
为什么 Window 样式在存在时不覆盖 UserControl 显式工具提示 属性,但在移除时会覆盖?
郑重声明,我还尝试更改 MyUserControl 以使用具有相同 x:Key
名称的 DynamicResource,但没有任何影响。
将两者都更改为 ToolTipService.ToolTip 也有相同的结果。
编辑:我修复了示例以证明即使未在本地级别设置工具提示,它仍然不会覆盖样式。
除非有人能回答如何覆盖工具提示的子样式,否则我最终将 ToolTip 对象设为 MyUserControl
上的依赖项 属性,并将其传递给 SomeControl
。
我做的依赖属性叫做ExampleToolTip
MyUserControl.xaml:
<UserControl x:Name='MainControl'>
<UserControl.Resources>
<ToolTip x:Key="DefaultSomeControlToolTip">
<TextBlock Text="FOO"/>
<ToolTip>
</UserControl.Resources>
<Grid>
<local:SomeControl ToolTip="{Binding ExampleToolTip, ElementName=MainControl, TargetNullValue={StaticResource DefaultSomeControlToolTip}}"/>
</Grid>
</UserControl>
Window.xaml:
<Grid>
<local:MyUserControl>
<local:MyUserControl.ExampleToolTip>
<ToolTip>
<TextBlock Text="BAR"/>
</ToolTip>
</local:MyUserControl.ExampleToolTip>
</local:MyUserControl>
</Grid>
现在我可以使用 MyUserControl
及其在 SomeControl
上的特殊 "FOO" 工具提示,Window
的 "BAR" 工具提示覆盖它。
鉴于我有一个来自某个名为 SomeControl
在 MyUserControl.xaml 中,我这样使用 SomeControl
:
<Grid.Resources>
<Window.Resources>
<Style TargetType="local:SomeControl">
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip>
<TextBlock Text="FOO"/>
</ToolTip>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
</Grid.Resources>
<Grid>
<!-- Others controls in here -->
<local:SomeControl />
</Grid>
在Window.xaml中:
<Window.Resources>
<Style TargetType="local:SomeControl">
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip>
<TextBlock Text="BAR"/>
</ToolTip>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<local:MyUserControl />
</Grid>
结果是我要它显示Bar
时它会显示Foo
。
如果我从 UserControl 中删除工具提示,将使用 Window 的样式,它会像我期望的那样显示 Bar
。
为什么 Window 样式在存在时不覆盖 UserControl 显式工具提示 属性,但在移除时会覆盖?
郑重声明,我还尝试更改 MyUserControl 以使用具有相同 x:Key
名称的 DynamicResource,但没有任何影响。
将两者都更改为 ToolTipService.ToolTip 也有相同的结果。
编辑:我修复了示例以证明即使未在本地级别设置工具提示,它仍然不会覆盖样式。
除非有人能回答如何覆盖工具提示的子样式,否则我最终将 ToolTip 对象设为 MyUserControl
上的依赖项 属性,并将其传递给 SomeControl
。
我做的依赖属性叫做ExampleToolTip
MyUserControl.xaml:
<UserControl x:Name='MainControl'>
<UserControl.Resources>
<ToolTip x:Key="DefaultSomeControlToolTip">
<TextBlock Text="FOO"/>
<ToolTip>
</UserControl.Resources>
<Grid>
<local:SomeControl ToolTip="{Binding ExampleToolTip, ElementName=MainControl, TargetNullValue={StaticResource DefaultSomeControlToolTip}}"/>
</Grid>
</UserControl>
Window.xaml:
<Grid>
<local:MyUserControl>
<local:MyUserControl.ExampleToolTip>
<ToolTip>
<TextBlock Text="BAR"/>
</ToolTip>
</local:MyUserControl.ExampleToolTip>
</local:MyUserControl>
</Grid>
现在我可以使用 MyUserControl
及其在 SomeControl
上的特殊 "FOO" 工具提示,Window
的 "BAR" 工具提示覆盖它。