鑑於我有一個控制從什麼地方叫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顯式ToolTip屬性,但在刪除時會如何?
爲了記錄,我也嘗試更改MyUserControl以使用名稱相同的x:Key
的DynamicResource並且沒有任何影響。
將兩者都更改爲ToolTipService.ToolTip也具有相同的結果。
編輯:我修正了這個例子,說明即使ToolTip沒有在本地級別上設置,它仍然不會覆蓋樣式。
爲什麼您的樣式是針對SomeControl的,但您的控件名爲MyUserControl? – SledgeHammer
因爲用戶控件內部的類型是'SomeControl'。我沒有試圖定位'MyUserControl',我試圖定位'SomeControl'。 'Window'有一個'UserControl',它有一個'SomeControl'工具提示在'SomeControl'上,而不是'UserControl' – user99999991