开源WPF-Samples-netframework学习(1)Data Binding——CollectionViewSource

案例地址:GitHub - microsoft/WPF-Samples: Repository for WPF related samples

一、运行界面

 二、案例功能描述

知识点:

1、 CollectionViewSource:可扩展应用程序标记语言 (XAML) 代理

        CollectionViewSource.SortDescriptions: 排序

        CollectionViewSource.GroupDescriptions:分组

2、XmlDataProvider:允许以声明方式访问数据绑定的 XML 数据

        XData:表示可以显示为的值的文本数据 Value 节点

3、在ListBox中,假如不设置ListBox.GroupStyle时,不会展示分组

        <ListBox.GroupStyle>
                <x:Static Member="GroupStyle.Default"/>
            </ListBox.GroupStyle>

三、分析代码

1、创建CollectionViewSource

        当source是ObservableCollection集合时

            <CollectionViewSource Source="{StaticResource Places}" x:Key="Cvs">
            <CollectionViewSource.SortDescriptions>
                <componentModel:SortDescription PropertyName="CityName"/>
            </CollectionViewSource.SortDescriptions>
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="State"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

        当source是XmlDataProvider时

         <CollectionViewSource x:Key="MySortedTasks1"
                              Source="{StaticResource MyTasks}">
            <CollectionViewSource.SortDescriptions>
                <componentModel:SortDescription PropertyName="@Priority" />
            </CollectionViewSource.SortDescriptions>
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="@Type" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

2、将CollectionViewSource绑定到ListBox

ListBox 的 DisplayMemberPath :获取或设置源对象上的值的路径,以用作对象的可视表示形式。

        <ListBox ItemsSource="{Binding Source={StaticResource Cvs}}"
             DisplayMemberPath="CityName" Name="lb"> 
            <ListBox.GroupStyle>
                <x:Static Member="GroupStyle.Default"/>
            </ListBox.GroupStyle>

        </ListBox>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下步骤将 ComboBox 的 SelectionChanged 事件绑定到 ViewModel 中的一个命令: 1. 在 ViewModel 中定义一个 ICommand 属性,用于处理 ComboBox 的 SelectionChanged 事件。 ```csharp private ICommand _comboBoxSelectionChangedCommand; public ICommand ComboBoxSelectionChangedCommand { get { if (_comboBoxSelectionChangedCommand == null) { _comboBoxSelectionChangedCommand = new RelayCommand<object>(ComboBoxSelectionChanged, CanComboBoxSelectionChanged); } return _comboBoxSelectionChangedCommand; } } private bool CanComboBoxSelectionChanged(object parameter) { // 可选:检查命令是否可用 return true; } private void ComboBoxSelectionChanged(object parameter) { // 处理 ComboBox 的 SelectionChanged 事件 } ``` 2. 在 XAML 中,将 ComboBox 的 SelectionChanged 事件绑定到上面定义的命令,并使用 CommandParameter 属性将 ComboBox 本身作为参数传递给命令。 ```xml <ComboBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" SelectionChanged="{Binding ComboBoxSelectionChangedCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding}" /> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> ``` 注意,在上面的代码中,我使用了一个 RelayCommand 类来实现 ICommand 接口,你可以根据需要使用其他实现方式来绑定命令。另外,我还使用了 CommandParameter 属性将 ComboBox 本身作为参数传递给命令,这样在 ViewModel 中就可以访问 ComboBox 的属性和方法了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值