【问题标题】:How to change ListBox DataTemplate in WPF based on CheckBox如何在 WPF 中基于 CheckBox 更改 ListBox DataTemplate
【发布时间】: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


【解决方案1】:

您的数据模板是在 app.xaml 中定义的 StaticResource,您正在尝试将元素名称绑定到同一范围内不存在的元素 IssueListBox。即便如此,你想要做的就是这个。 Listbox 有一个数据模板 DT,在 DT 中您试图返回到 List box 并将其 DataTemplate 设置为另一个(不是 DT)。

为什么不组合模板,将详细信息的可见性设置为折叠并根据您的属性触发可见性。那么您根本不必引用列表框,模板保持不变,当您想查看详细信息时,它只是在内部发生变化。

【讨论】:

  • 成功了,尽管我仍然想知道如果我需要两个模板,我将如何处理它。当我刚刚向 check.changed 事件添加一个事件处理程序时,生活变得轻松多了,但我正在努力养成更好的习惯,因为我正在学习并在学习 xaml 时尽可能地使事情成为 MVVMish。
  • 您可以使用数据模板选择器,但这是在添加数据模板时在数据模板之间做出决定,而不是在属性更改时做出决定。
猜你喜欢
  • 2011-07-13
  • 1970-01-01
  • 2019-10-18
  • 1970-01-01
  • 2011-05-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-27
  • 1970-01-01
相关资源
最近更新 更多