简体   繁体   中英

WPF DataTrigger change control

I have a ListView that is filled by an ObservableCollection<MenuTrayItem> . In my resources I've defined a DataTemplate for that class. I want to catch the trigger on the ListViewItem control and change the background of a Border control in my DataTemplate .

The error that I'm getting is

{"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>

The border is outside the scope of the setter.

From MSDN :

You can set this property to the name of any element within the scope of where the setter collection (the collection that this setter is part of) is applied. This is typically a named element that is within the template that contains this setter.

You could possibly place the trigger in the Style of the Border itself. looking for the ListViewItem from the inside. As this couples the UserControl to the use in ListViewItems that might not be wanted so you could also create an interface-property on the UserControl itself which is used by the DataTrigger which again will be on the Border but the property will be set from outside.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM