带有复选框的 Wpf MVVM 组合框并选择所有复选框

我有一个带复选框的组合框,我想实现全选选项。我在 XAML 中按以下方式执行此操作:


<ComboBox Text="Select Industry"  TextSearch.TextPath ="Industry"  Name="industry"  IsEditable="True" IsReadOnly="True" >

    <ComboBox.ItemsSource>

        <CompositeCollection>

            <ComboBoxItem>

                <CheckBox x:Name="allIndustry">All</CheckBox>

            </ComboBoxItem>

            <CollectionContainer Collection="{Binding Source={StaticResource industrySource}}"/>

        </CompositeCollection>

    </ComboBox.ItemsSource>

    <ComboBox.ItemTemplate>

        <DataTemplate>

            <StackPanel Orientation="Horizontal">

                <CheckBox Name="industry" IsChecked="{Binding ElementName=allIndustry, Path=IsChecked, Mode=OneWay}" Content="{Binding Industry}" />

            </StackPanel>

        </DataTemplate>

    </ComboBox.ItemTemplate>

</ComboBox>

我使用上面的代码在视图中获得了这个功能:

http://img3.mukewang.com/60bad2f700015fa306830454.jpg

但是,这里的问题是我曾经绑定 ViewModel 属性 IsChecked 的 IsChecked ComboBox 属性,而实施此解决方案时我失去了此功能。


我想移动线


IsChecked="{Binding ElementName=allIndustry, Path=IsChecked, Mode=OneWay}"

进入


<ComboBoxItem>

<CheckBox x:Name="allIndustry">All</CheckBox>

</ComboBoxItem>

将绑定更改为 OneWayToSource,并从x:Name="allIndustry" CheckBox 中的 My Selected Items更新。


我应该只能从 XAML 视图中执行此操作...


之后,我将我的 ComboBox 绑定到 ViewModel 属性......


Qyouu
浏览 460回答 2
2回答

慕少森

如果没有完整的工作示例,就不容易给出完整的工作示例。您可以使用其他方法解决您的问题。你IndustryFilters不应该是ObservableCollection<IndustryFilter>这样的对象的实例:public class IndustryFilters : INotifyPropertyChanged {&nbsp; &nbsp; private _isAllChecked;&nbsp; &nbsp; public IsAllChecked {&nbsp; &nbsp; &nbsp; &nbsp; get {return _isAllChecked;}&nbsp; &nbsp; &nbsp; &nbsp; set{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _isAllChecked = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach(var filter in Filters) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filter.IsChecked = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PropertyChanged(...);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public ObservableCollection<IndustryFilter> Filters&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get { return _industryFilters; }&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _industryFilters = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PropertyChanged(this, new propertyChangedEventArgs("IndustryFilters"));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}然后将IsCheckedof绑定<CheckBox x:Name="allIndustry">All</CheckBox> 到IsAllChecked属性。然后,您必须找到一种方法将 ComboBox 的来源更改为 IndustryFilters.Filters。希望这可以帮助。

缥缈止盈

这是 State 过滤器的 ViewModel 属性定义 - 与我询问的 Industry 字段相同:private ObservableCollection<StateFilter> _stateFilters;&nbsp; &nbsp; &nbsp; &nbsp; public ObservableCollection<StateFilter> StateFilters&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get { return _stateFilters; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _stateFilters = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PropertyChanged(this, new PropertyChangedEventArgs("StateFilters"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private bool _stateFilter;&nbsp; &nbsp; &nbsp; &nbsp; public bool StateFilter&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get { return _stateFilter; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _stateFilter = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ObservableCollection<StateFilter> local = new ObservableCollection<StateFilter>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var filter in StateFilters)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filter.IsChecked = _stateFilter;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local.Add(filter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StateFilters = local;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PropertyChanged(this, new PropertyChangedEventArgs("StateFilter"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }这是 XAML 代码示例:资源:组合框:<ComboBox Text="Select State"&nbsp; TextSearch.TextPath ="State"&nbsp; Name="state"&nbsp; IsEditable="True" IsReadOnly="True" >&nbsp; &nbsp; <ComboBox.ItemsSource>&nbsp; &nbsp; &nbsp; &nbsp; <CompositeCollection>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ComboBoxItem >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <CheckBox&nbsp; Name="all" IsChecked="{Binding StateFilter}">All</CheckBox>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ComboBoxItem>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <CollectionContainer Collection="{Binding Source={StaticResource stateSource}}"/>&nbsp; &nbsp; &nbsp; &nbsp; </CompositeCollection>&nbsp; &nbsp; </ComboBox.ItemsSource>&nbsp; &nbsp; <ComboBox.ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <CheckBox Name="chkTask" IsChecked="{Binding IsChecked}"&nbsp; Content="{Binding State}" ></CheckBox>&nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate></ComboBox.ItemTemplate></ComboBox>
打开App,查看更多内容
随时随地看视频慕课网APP