【发布时间】:2011-01-04 11:27:00
【问题描述】:
我正在尝试应用 DataTrigger 将 DataTemplate 更改为 ListBox 并收到错误消息:
"Error 1 Cannot find the Trigger target 'IssueListBox'. (The target must appear before any Setters, Triggers, or Conditions that use it.)"
我的主要Window 中有一个ListBox(在DockPanel 以及其他控件中):
<ListBox x:Name="IssueListBox"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ShowIssueSimple}"
IsSynchronizedWithCurrentItem="True"
HorizontalContentAlignment="Stretch"
BorderThickness="3" DockPanel.Dock="Top"
VerticalContentAlignment="Stretch" Margin="2"/>
我在 App.xaml 中有一对DataTemplates,第二个模板底部有一个DataTrigger:
<DataTemplate x:Key="ShowIssueDetail">
<Border CornerRadius="4, 8, 4, 8" Margin="2" MinWidth="400" BorderThickness="3"
BorderBrush="{Binding Path=IssUrgency, Converter={StaticResource IntToRYGBBoarderBrushConverter}}">
<StackPanel Orientation="Horizontal">
<StackPanel Margin="10">
<TextBlock Text="{Binding IssSubject}" FontWeight="Bold" FontSize="14"/>
<StackPanel Width="Auto" Orientation="Horizontal">
<TextBlock Text="Due: " FontWeight="Bold"/>
<TextBlock Text="{Binding IssDueDate}" FontStyle="Italic" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Width="Auto" Orientation="Horizontal">
<TextBlock Text="Category: " FontWeight="Bold"/>
<TextBlock Text="{Binding IssCategory}"/>
</StackPanel>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
<DataTemplate x:Key="ShowIssueSimple">
<Border CornerRadius="6"
Margin="2,1,2,1"
MinWidth="400"
BorderThickness="2"
SnapsToDevicePixels="True"
BorderBrush="{Binding Path=IssUrgency, Converter={StaticResource IntToRYGBBoarderBrushConverter}}">
<StackPanel Margin="5">
<TextBlock Text="{Binding IssSubject}" FontWeight="Bold" FontSize="14"/>
</StackPanel>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Source={StaticResource sbvm}, Path=ShowDetailListItems}" Value="True">
<Setter TargetName="IssueListBox" Property="ItemTemplate" Value="{StaticResource ShowIssueDetail}"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
如何让Trigger 工作?谷歌先生让我失望了,这样的例子比比皆是,但它们不是基于另一个控件。
【问题讨论】:
-
在 DataTrigger 中,您将 ItemTemplate 设置为包含它的 DataTemplate - ShowIssueSimple。这只是一个错字吗?我猜你的意思是 Value="{StaticResource ShowIssueDetail}"。
-
是的,是一个“错字”(:感谢提醒,已修复
标签: wpf xaml listbox datatemplate datatrigger