如何在WPF中基于CheckBox更改ListBox DataTemplate

发布于 2024-08-17 19:57:31 字数 2869 浏览 7 评论 0原文

我正在尝试应用 DataTrigger 更改 ListBoxDataTemplate 并收到错误:

“错误 1 ​​找不到触发器目标“IssueListBox”。(该目标必须出现在使用它的任何 Setter、触发器或条件之前。)”

我的主 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 中有一对 DataTemplate,其底部有一个 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>

如何让触发器工作?谷歌先生让我失望了,这样的例子比比皆是,但它们并不是基于另一种控件。

I am trying to apply a DataTrigger to change the DataTemplate for a ListBox and am getting the error:

"Error 1 Cannot find the Trigger target 'IssueListBox'. (The target must appear before any Setters, Triggers, or Conditions that use it.)"

I have a ListBox in my main Window (In a DockPanel along with other controls):

<ListBox x:Name="IssueListBox"
  ItemsSource="{Binding}"
    ItemTemplate="{StaticResource ShowIssueSimple}" 
    IsSynchronizedWithCurrentItem="True"
    HorizontalContentAlignment="Stretch" 
    BorderThickness="3" DockPanel.Dock="Top" 
    VerticalContentAlignment="Stretch" Margin="2"/>

I have a pair of DataTemplates in App.xaml with a DataTrigger at the bottom of the 2nd template:

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

How do I get the Trigger to work? Mr Google has failed me, many examples like this abound but they are not based on another control.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咋地 2024-08-24 19:57:31

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

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

Your data template is a StaticResource defined in app.xaml, you are trying to do an element name binding to the element IssueListBox which doesnt exist in the same scope. Even then what you are trying to do is this. Listbox has a data template DT, inside DT you are trying to reach back to the List box and set its DataTemplate to another one (not DT).

Why dont you combine the templates, set the visibility on the details to collapsed and trigger the visibility based on your property. then you dont have to reference the list box at all and the template stays the same, it just changes internally when you want to see the details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文