我想以编程方式(在code / not xaml中)为每个“TreeViewItem”样式分配一个新的DataTrigger,就像我在下面的xaml中一样。
我做了一些代码(在xaml下)我定义了我的触发器,但是“ItemContainerStyle”为null。该函数在Window Initialize事件上调用。
任何人都知道我做错了什么?
修改
我发现了一些问题:我将我的样式从“资源”部分移动到“ItemContainerStyle”部分,如下面代码示例中所定义。这样风格仍然应用,我可以从代码中的TReeView.ItemContainerStyle属性访问该样式。但我仍然不知道如何通过代码
获取TreeViewItem选择的样式,如背景颜色我有xaml:
<TreeView Name="TreeViewSelectScopeStudy" MinHeight="24" Margin="7"
ItemsSource="{Binding Path=TvItemRootPssTreeViewRoot.ChildsView}" Height="Auto"
VerticalAlignment="Stretch"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling">
<TreeView.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding Path=IsExpanded}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsHilighted}" Value="true">
<!--<Setter Property="Background" Value="SlateBlue"></Setter>-->
<Setter Property="Background" Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
我有那段代码:
public static void EnableMultipleSelection(TreeView treeView)
{
if (!_isMultiSelectTreeViewLeftButtonHandlerRegistered)
{
EventManager.RegisterClassHandler(typeof(TreeViewItem), UIElement.MouseDownEvent,
new MouseButtonEventHandler(TreeViewMouseDownGlobal));
_isMultiSelectTreeViewLeftButtonHandlerRegistered = true;
}
DataTrigger dataTrigger = new DataTrigger();
dataTrigger.Binding = new Binding("IsHilighted");
dataTrigger.Value = true;
dataTrigger.Setters.Add(new Setter(TreeViewItem.BackgroundProperty, new SolidColorBrush(Colors.Brown)));
treeView.ItemContainerStyle.Triggers.Add(dataTrigger);
已编辑的代码:
<TreeView Name="TreeViewSelectScopeStudy" MinHeight="24" Margin="7"
ItemsSource="{Binding Path=TvItemRootPssTreeViewRoot.ChildsView}" Height="Auto"
VerticalAlignment="Stretch"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding Path=IsExpanded}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsHilighted}" Value="true">
<Setter Property="Background" Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<!--<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding Path=IsExpanded}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsHilighted}" Value="true">
<Setter Property="Background" Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>-->
答案 0 :(得分:0)
在模板应用之前,听起来你很快就会尝试设置Style.Trigger。如果您在定位的FrameWorkElement
已完全加载后应用触发器,则可能会发现ItemStyleContainer
不再为空。
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.loaded.aspx
为OnApplyTemplate
实现FrameworkElement
应该在设置ItemStyleContainer之后将其挂钩,因为在呈现Visual Tree之后调用它。
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.onapplytemplate.aspx