因此,我在使用自定義 treeviewitem:s 制作自定義樹視圖時遇到了這個問題,其中ItemContainerStyle通過從自定義樣式加載樣式來清除。
它是這樣作業的。我有基于 TreeViewItem 的自定義 MyTreeViewItem。
<TreeViewItem x:Class="UI.MyTreeViewItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<TreeViewItem.Resources>
<Style x:Key="MyTreeViewItemStyle" TargetType="TreeViewItem">
<Setter Property="Background" Value="#AEFFC1" />
</Style>
</TreeViewItem.Resources>
</TreeViewItem>
正如你所看到的,我只是在這里做了一個簡單的著色,以確保它自己的樣式有效。除非我在后面的代碼中這樣做,否則這永遠不會加載。
編輯:我知道不需要把著色之類的東西放在這里,因為這里本來是一個模板。自從注意到真正起作用以來,我只是將其剝離到骨頭,以確保我放了一些超級簡單的東西,因為我知道應該可以作業,以防萬一它是因為模板本身。
public partial class MyTreeViewItem : TreeViewItem
{
public MyTreeViewItem()
{
InitializeComponent();
this.Loaded = MyTreeViewItem_Loaded;
}
private void MyTreeViewItem_Loaded(object sender, RoutedEventArgs e)
{
this.Style = Resources["MyTreeViewItemStyle"] as Style;
}
}
這作業爐排。已經多次與其他控制元件一起使用它來為需要加載的控制元件自定義樣式,而不必費心一遍又一遍地“重新設計”所有內容。
我遇到過這個問題。這就是ItemContainerStyle使用這種自定義樣式控制器的時候。
<local:BaseTreeView x:Class="My.Navigator.NavigatorTreeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ui="clr-namespace:UI;assembly=BaseCode"
d:DesignHeight="450" d:DesignWidth="800">
<ui:MyTreeView ItemsSource="{Binding Path=Nodes}">
...
<ui:MyTreeView.ItemContainerStyle>
<Style TargetType="{x:Type ui:MyTreeViewItem}">
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<EventSetter Event="Selected" Handler="TreeView_SelectedItemChanged" />
<EventSetter Event="Expanded" Handler="TreeView_NodeExpanded" />
<EventSetter Event="Collapsed" Handler="TreeView_NodeCollapsed" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</ui:MyTreeView.ItemContainerStyle>
</ui:MyTreeView>
</local:BaseTreeView>
這 ui:MyTreeView.ItemContainerStyle一次的風格被加載為您在上面看到被完全忽視,通過this.Style = Resources["MyTreeViewItemStyle"] as Style;在MyTreeViewItem_Loaded。這意味著這些 Setter、EventSetter 和 Triggers 根本不會觸發,因為它們仍然需要能夠作為附加規則添加。
如何解決這個問題,以便可以加載自定義控制元件中的預定義樣式,并且通過使用此控制元件,您仍然可以連接獨特的規則,就像上面一樣,而不會被預定義的規則否決?
uj5u.com熱心網友回復:
目前還不清楚你為什么要做你正在做的事情。我只能說,Style在使用TreeView.ItemContainerStyle.
通常,在 a 上UserControl,您將在元素上本地設定屬性:
<TreeViewItem x:Class="UI.MyTreeViewItem"
...
d:DesignHeight="450" d:DesignWidth="800"
Background="#AEFFC1">
</TreeViewItem>
或在代碼隱藏中:
private void MyTreeViewItem_Loaded(object sender, RoutedEventArgs e)
{
this.Background =
new SolidColorBrush(ColorConverter.ConvertFromString("#AEFFC1"));
}
撰寫自定義 時Control,您將Style在 Generic.xaml 中提供默認值。這是最好的解決方案,因為它允許設定控制元件的樣式(允許自定義樣式覆寫 default 提供的默認值Style)。外部樣式被隱式合并。您應該更喜歡自定義控制元件而不是 UserControl。
您當前的代碼不允許樣式,因為您強行覆寫了自定義提供的值Style:
// Overwrite previous property value.
this.Style = someValue;
這是編程 101,一年級:賦值總是覆寫變數的舊值(參考)。
假設您知道自己在做什么并且不想使用上述解決方案之一,則必須使用 Style.BasedOn 屬性手動合并兩種樣式:
private void MyTreeViewItem_Loaded(object sender, RoutedEventArgs e)
{
var defaultStyle = Resources["MyTreeViewItemStyle"] as Style;
defaultStyle.BasedOn = this.ItemContainerStyle;
this.Style = defaultStyle;
}
請參閱:控制元件創作概述:控制元件創作模型
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/315336.html
