![](/img/trans.png)
[英]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.