У меня есть ListView
, который заполнен ObservableCollection<MenuTrayItem>
. В моих ресурсах я определил DataTemplate
для этого класса. Я хочу перехватить триггер на элементе управления ListViewItem
и изменить фон элемента управления Border
в моем DataTemplate
.
Я получаю ошибку
{"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>