获取ListView的选定项(MVVM,Caliburn.Micro) [英] Get the selected items of a ListView (MVVM, Caliburn.Micro)

查看:115
本文介绍了获取ListView的选定项(MVVM,Caliburn.Micro)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况如何:

我正在使用Caliburn.Micro开发C#WPF应用程序.我正在使用MVVM模式.

I'm working on a C# WPF application with Caliburn.Micro. I am using the MVVM pattern.

我有一个ContentView作为ItemTemplate的ListView.ListView的ItemsSource绑定到相应ViewModel中的ViewModel列表(ObservableCollection).

I have a ListView with a ContentControl as ItemTemplate. The ListView's ItemsSource is bound to a List (ObservableCollection) of ViewModels in the corresponding ViewModel.

<ListView ItemsSource="{Binding ViewModelList}" SelectionMode="Extended">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ContentControl cal:View.Model="{Binding}" />
            </DataTemplate>
        </ListView.ItemTemplate>
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    </ListView>


我想要的东西:

我想获取ListView的选定项(全部),但是我不知道如何避免违反MVVM模式.

I want to get the selected items (all of them) of the ListView, but I don't know how without violating the MVVM pattern.

最好在ContentControl呈现的ViewModels中具有属性"IsSelected",并将其以某种方式绑定到我的ListView.

It would be nice to have a property "IsSelected" in the ViewModels that are presented by the ContentControl and to bind that somehow to my ListView.

是否可以这样做?或者还有其他/更好的方法吗?

Is it possible to do that or is there another/a better way?

更新:

这比预期的要容易.我添加了一个属性 public bool IsSelected {get;放;} 放在我的ViewModel中,然后将其放在ListView控件中:

It was easier than expected. I added a property public bool IsSelected { get; set; } in my ViewModel and put this inside the ListView Control:

<UserControl.Resources>
        <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource {x:Type ListViewItem}}">
            <Setter Property="IsSelected" Value="{Binding IsSelected}" />
        </Style>
</UserControl.Resources>

现在,我可以通过以下方式获取所选项目:

Now I can get the selected items with:

foreach (var item in ViewModelList)
{
     if (item.IsSelected)
     {
          // Do stuff
     }
}

推荐答案

<Grid>
    <Grid.Resources>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=OneWayToSource}"/>
        </Style>
    </Grid.Resources>
    <ListView ItemsSource="{Binding ViewModelList}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding IsSelected}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal">
                </StackPanel>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    </ListView>
</Grid>

这篇关于获取ListView的选定项(MVVM,Caliburn.Micro)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆