【问题标题】:Change databinding on WPF ComboBox when Checkbox value changes复选框值更改时更改 WPF ComboBox 中的数据绑定
【发布时间】:2009-03-04 23:07:34
【问题描述】:

我有一个 WPF 组合框,它数据绑定到一个集合,但根据是否选中复选框,我想改变组合框绑定到哪个集合。

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

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

【问题讨论】:

    标签: wpf data-binding


    【解决方案1】:

    这是一个使用 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,但原理是相同的。

    【讨论】:

      【解决方案2】:

      我知道的最好方法是使用一些在内部“获取”正确集合的 shell 集合。

      所以你有你的 UnfilteredCollection 和你的 FilteredCollection,然后是一个名为 BindingCollection 的属性,它在它的“getter”中评估某个状态(复选框将绑定到该状态)以确定要检索的集合。

      如果您使用 MVVM 在 UI 和集合之间进行数据绑定,一种方法是这样的:

      <!-- Your ComboBox binds to some shell collection -->
      <ComboBox ItemsSource="{Binding BindingCollection}" />
      <!-- The input to this item will determine which collection is internally exposed -->
      <CheckBox IsChecked="{Binding UseFilteredSet}" />
      

      然后让你的 ViewModel(中间层)文件做这样的事情(我不包括 INotifyPropertyChanged 的​​实现细节,但如果你愿意,我可以):

      private ObservableCollection<MyCustomer> UnfilteredCollection
      {
          get { return _unfilteredCollection; }
      }
      
      private ObservableCollection<MyCustomer> FilteredCollection
      {
          get { return _filteredCollection; }
      }
      
      // The public collection to which your ComboBox is bound
      public ObservableCollection<MyCustomer> BindingCollection
      {
          get 
          { 
              return UseFilteredSet ? 
                  FilteredCollection : 
                  UnfilteredCollection; 
          }
      }
      
      // CheckBox is bound to this state value, which tells the bindings on the shell 
      // collection to refresh when the value of this state changes.
      public bool UseFilteredSet
      {
          get { return _useFilteredSet; }
          set 
          { 
              _useFilteredSet = value;
              OnPropertyChanged("UseFilteredSet");
              OnPropertyChanged("BindingCollection");
          }
      }
      

      【讨论】:

      • 谢谢 KP - 我昨天有这个想法,但希望有一个“更纯”的 xaml 解决方案 - 也许我在找错树,而纯 xaml 解决方案不是正确的方法首先是关于事情的。
      • 好吧,唯一想到的另一件事是某种基于触发器的方法,它根据复选框的 IsChecked 属性更改“ItemsSource”属性,但我不知道如何获得它工作。
      猜你喜欢
      • 1970-01-01
      • 2015-09-14
      • 2010-10-12
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      相关资源
      最近更新 更多