【问题标题】:WPF ListView multiselection using custom CheckBox insideWPF ListView 多选使用自定义 CheckBox 里面
【发布时间】:2016-07-23 13:41:14
【问题描述】:

我有一个相当复杂的对象模型,其中包含嵌套的自定义集合对象/模型,如下所示:

public sealed class LibraryInfo : NamedModel
{       
    public ClassInfos _classes;       
    public ClassInfos Classes
    {
        get { return _classes; }
        set { SetProperty(ref _classes, value); }
    }
}

public class ClassInfos : List<ClassInfo> { }

public sealed class ClassInfo : NamedModel
{
    public PropertyInfos _properties;     
    public PropertyInfos Properties
    {
        get { return _properties; }
        set { SetProperty(ref _properties, value); }
    }
}

public class PropertyInfos : List<PropertyInfo> { }

public sealed class PropertyInfo : NamedModel
{
}

我想将LibraryInfo 类绑定到一个ListView 以进行类选择。

<ListView SelectedItem="{Binding SelectedClass}" ItemsSource="{Binding LibraryInfo.Classes}">
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
        </GridView>
    </ListView.View>
</ListView>

基于ClassInfo 选择,我想在另一个ListView 上显示所选类的属性以进行属性选择。

<ListView ItemsSource="{Binding SelectedClass.Properties}">
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
        </GridView>
    </ListView.View>        
</ListView>

问题是我如何跟踪已检查(CheckBox IsChecked)项目,因为我的原始模型不包含任何此类属性,如 IsActive,以保持 UI 相关字段与我的模型分离。

我正在寻找一个优雅而简单的解决方案来解决这个问题。

【问题讨论】:

  • 正如你所说,这是一个模型类。要跟踪 UI 更改,您需要一个 ViewModel 类。与其直接绑定你的模型,不如用一个视图模型包装它并绑定到那个类。
  • @qqww2,我知道它应该被包装在 ViewModel 中,但是这样 ViewModel 变得相当复杂。我正在寻找简单的解决方案。能否请您具体使用代码提供指导。

标签: wpf xaml listview data-binding multi-select


【解决方案1】:

使用混合行为。

在您的ViewModel 中为选定的项目使用Collection

编写一个名为UpdateSelectedItemsCommandICommand 并传递1/0 参数用于添加/删除项目。

        <CheckBox ...>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Checked">
                    <i:InvokeCommandAction Command="{Binding UpdateSelectedItemsCommand}"
                                                            CommandParameter="1"/>
                </i:EventTrigger>
                <i:EventTrigger EventName="Unchecked">
                    <i:InvokeCommandAction Command="{Binding UpdateSelectedItemsCommand}"
                                                            CommandParameter="0"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </CheckBox>

【讨论】:

    猜你喜欢
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 2013-07-25
    • 2023-03-18
    相关资源
    最近更新 更多