ListView SelectedItems MVVM中的DataBinding实现

时间:2016-12-28 13:06:32

标签: c# wpf listview data-binding custom-controls

我有一个问题是绑定ListView控件的SelectedItems,为了解决它我扩展了ListView控件,ListViewExtended使用来自此post的引用公开SelectedItemsList但是双向绑定不是努力在模型中获取SelectedItems。

请帮助指出此演示源中的问题。

自定义控制:

public sealed class ListViewExtended : ListView
{
    public ListViewExtended()
    {
        this.SelectionChanged += ListViewExtended_SelectionChanged;
    }

    void ListViewExtended_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        this.SelectedItemsList = this.SelectedItems;
    }

    #region SelectedItemsList

    public IEnumerable SelectedItemsList
    {
        get { return (IEnumerable)GetValue(SelectedItemsListProperty); }
        set { SetValue(SelectedItemsListProperty, value); }
    }

    public static readonly DependencyProperty SelectedItemsListProperty =
            DependencyProperty.Register("SelectedItemsList",
            typeof(IEnumerable),
            typeof(ListViewExtended),
            new FrameworkPropertyMetadata(null));

    #endregion
}

型号:

public class Member
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Family : BindableBase
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; OnPropertyChanged("Name"); }
    }

    private IList<Member> _selectedMembers;
    public IList<Member> SelectedMembers
    {
        get { return _selectedMembers; }
        set { _selectedMembers = value; OnPropertyChanged("SelectedMembers"); }
    }

    public override string ToString()
    {
        return Name;
    }
}

视图模型:

public class ListViewSelectedItemsExtendedViewModel : BindableBase
{
    Dictionary<string, IEnumerable> _repository = new Dictionary<string, IEnumerable>();

    public ListViewSelectedItemsExtendedViewModel()
    {
        var families = new List<Family>();
        families.Add(new Family() { Name = "family 1" });
        families.Add(new Family() { Name = "family 2" });
        Families = families;

        var items = new List<Member>();
        items.Add(new Member() { Name = "John", Age = 30 });
        items.Add(new Member() { Name = "Chapel", Age = 50 });
        items.Add(new Member() { Name = "Max", Age = 46 });
        _repository.Add(families[0].Name, items);

        items = new List<Member>();
        items.Add(new Member() { Name = "Warner", Age = 28 });
        items.Add(new Member() { Name = "Peter", Age = 36 });
        items.Add(new Member() { Name = "Tom", Age = 5 });
        _repository.Add(families[1].Name, items);
    }

    private IList<Family> _families;
    public IList<Family> Families
    {
        get { return _families; }
        set { _families = value; OnPropertyChanged("Families"); }
    }

    private Family _selectedFamily;
    public Family SelectedFamily
    {
        get { return _selectedFamily; }
        set { _selectedFamily = value; OnPropertyChanged("SelectedFamily"); }
    }

    private ICommand _familySelectionChangedCommand;
    public ICommand FamilySelectionChangedCommand
    {
        get
        {
            return _familySelectionChangedCommand ?? (_familySelectionChangedCommand = new DelegateCommand(
                () =>
                {
                    Members = (IList<Member>)_repository[SelectedFamily.Name];
                }
            ));
        }
    }
}

XAML:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="3*" />
    </Grid.ColumnDefinitions>
    <ListBox ItemsSource="{Binding Families}" SelectedItem="{Binding SelectedFamily}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding FamilySelectionChangedCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ListBox>
    <Grid Grid.Column="1">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <fsx:ListViewExtended Margin="10" Grid.Row="0"
                            ItemsSource="{Binding Members}" 
                            SelectedItemsList="{Binding SelectedFamily.SelectedMembers, Mode=TwoWay}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Include">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding Path=IsSelected, 
                                RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
                </GridView>
            </ListView.View>
            <!--<i:Interaction.Behaviors>
            <fsx:ListViewMultiSelectionBehavior 
                SelectedItems="{Binding SelectedFamily.SelectedMembers}" />
        </i:Interaction.Behaviors>-->
        </fsx:ListViewExtended>
        <ListView Margin="10" Grid.Row="1" 
                ItemsSource="{Binding SelectedFamily.SelectedMembers}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Grid>

除了上述实施,我还尝试使用此post中给出的方法,但没有运气获得100%的结果。

1 个答案:

答案 0 :(得分:0)

此处的问题是您无法设置泛型类型IList&lt; Member&gt;的源属性。到非通用IList,它是ListView的SelectedItems属性的类型。如果您自己尝试,您会发现它甚至无法编译:

IList<Member> members = listView1.SelectedItems; //WON'T COMPILE

通用IList&lt; T&gt;接口不扩展IList接口,因此它们是两种完全不同的类型。这就像尝试将int属性设置为字符串。它不会起作用。

如果您将Family类的SelectedMembers属性的类型更改为非通用IList,它将起作用:

public class Family : BindableBase
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; OnPropertyChanged("Name"); }
    }

//change the type here:
    private IList _selectedMembers;
    public IList SelectedMembers
    {
        get { return _selectedMembers; }
        set { _selectedMembers = value; OnPropertyChanged("SelectedMembers"); }
    }

    public override string ToString()
    {
        return Name;
    }
}
  

必须有一些方法来概括它,就像其他内置控件的DependencyProperty一样使用集合

如上所述;为了能够将source属性设置为ListView的SelectedItems属性的值, source 属性的类型必须与SelectedItems属性的类型匹配,即source属性应该是非通用的IList。

您可能还想阅读此内容:https://blog.magnusmontin.net/2014/01/30/wpf-using-behaviours-to-bind-to-readonly-properties-in-mvvm/。它是关于如何使用行为绑定到只读属性。无论您选择采用何种解决方案,属性类型仍必须匹配。