[英]wpf: change a style DataTrigger to a ControlTemplate DataTrigger
[英]WPF DataTrigger change control
我有一个由ObservableCollection<MenuTrayItem>
填充的ListView
。 在我的资源中,我已经为该类定义了一个DataTemplate
。 我想在ListViewItem
控件上捕获触发器,并在DataTemplate
更改Border
控件的背景。
我得到的错误是
{"Child with Name 'Container' not found in VisualTree."}
GlobalResources.xaml
<DataTemplate x:Key="MenuTrayItem_Template" DataType="{x:Type model:MenuTrayItem}">
<view:MenuTrayItemView Margin="5,0,5,0" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListViewItem}},Path=IsSelected}" Value="True">
<!-- i am trying to change the background on the control "Container"
in the <view:MenuTrayitemView /> -->
<Setter TargetName="Container" Property="Border.Background" Value="Red" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
/Views/MenuTrayItemView.xaml
<UserControl x:Class="CellestusInvoicing.Views.MenuTrayItemView"
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">
<UserControl.Resources>
<ResourceDictionary Source="/Resources/GlobalResources.xaml" />
</UserControl.Resources>
<Border x:Name="Container" Width="64" Height="48" CornerRadius="5" Background="{StaticResource Gradient_Grey}" Cursor="Hand" MouseEnter="Container_MouseEnter" MouseLeave="Container_MouseLeave">
<StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Image Margin="0,3,0,0" Width="60" Height="30" Source="{Binding Image, FallbackValue='/Images/Icons/MenuTray_Home.png'}" />
<TextBlock Margin="3,0,0,0" Text="{Binding Title, FallbackValue='title'}" FontSize="10" Foreground="White" />
</StackPanel>
</Border>
</UserControl>
边界不在设置员的范围内。
来自MSDN :
您可以将此属性设置为应用setter集合(此setter所属的集合)的范围内的任何元素的名称。 这通常是包含此setter的模板内的命名元素。
您可以将触发器放置在“边框样式”本身中。 从内部寻找ListViewItem。 由于这会将UserControl与可能不需要的ListViewItems结合使用,因此您还可以在DataTrigger使用的UserControl自身上创建接口属性,该属性再次位于Border上,但该属性将从外部设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.