gpt4 book ai didi

wpf - 复选框值更改时更改 WPF ComboBox 中的数据绑定(bind)

转载 作者:行者123 更新时间:2023-12-02 07:56:36 26 4
gpt4 key购买 nike

我有一个数据绑定(bind)到集合的 WPF ComboBox,但是根据是否选中复选框,我想改变 ComboBox 绑定(bind)到哪个集合。

基本问题是我有大量的 MyCustomer 集合,我还有一个经过过滤的 MyCustomer 集合 - 过滤非常密集,我不想用 CollectionView 来做,主要原因是它已经完成,过滤的集合已经存在 - 因此需要简单地切换组合的数据绑定(bind)。

我希望有一个纯 XAML 解决方案,显然在后面编写一些代码是一个相对简单的解决方案,但我觉得这不是必需的。

最佳答案

下面是一个使用 DataTrigger 切换集合的示例:

<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel.Resources>
<x:Array x:Key="notes" Type="{x:Type sys:String}">
<sys:String>do</sys:String>
<sys:String>re</sys:String>
<sys:String>mi</sys:String>
</x:Array>
<x:Array x:Key="letters" Type="{x:Type sys:Char}">
<sys:Char>a</sys:Char>
<sys:Char>b</sys:Char>
<sys:Char>c</sys:Char>
</x:Array>
</StackPanel.Resources>

<CheckBox x:Name="chkLetters" Content="Use Letters"/>
<ListBox>
<ListBox.Style>
<Style TargetType="{x:Type ListBox}">
<Setter Property="ItemsSource" Value="{StaticResource notes}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=chkLetters}" Value="True">
<Setter Property="ItemsSource" Value="{StaticResource letters}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.Style>
</ListBox>
</StackPanel>

对您而言,这些不会是不同的数组,而可能是带有过滤器或其他东西的不同 CollectionViewSources,但原理是相同的。

关于wpf - 复选框值更改时更改 WPF ComboBox 中的数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/612909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com