mvvm设计模式 listview绑定ICollectionView对象 如何获取多选数据

mdh524 2011-06-30 11:10:13
在mvvm模式下页面与代码是分离的,在viewmodel 下可以通过
OrdersView.CurrentChanged += 事件
方式来获取单选,如果做到获取多选的值。

类似以下代码

public ObservableCollection<OrderViewModel> Orders
{
get;
private set;
}
public ICollectionView OrdersView
{
get
{
if( _ordersView == null )
_ordersView = CollectionViewSource.GetDefaultView( Orders );
return _ordersView;
}
}
private ICollectionView _ordersView;
public OrderViewModel CurrentOrder
{
get { return OrdersView.CurrentItem as OrderViewModel; }
set { OrdersView.MoveCurrentTo( value ); }
}

<ListView ItemsSource="{Binding Path=OrdersView}" IsSynchronizedWithCurrentItem="True">
...全文
673 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
namhyuk 2011-07-04
  • 打赏
  • 举报
回复
我盗取一段摘自网上的代码,你可以参考一下:

设有:CustomerViewModel, AllCustomerViewModel二个ViewModel,


<Style x:Key="CustomerItemStyle" TargetType="{x:Type ListViewItem}">
<!--
Bind the IsSelected property of a ListViewItem to the
IsSelected property of a CustomerViewModel object.
-->
<Setter Property="IsSelected" Value="{Binding Path=IsSelected,
Mode=TwoWay}" />
</Style>



// In AllCustomersViewModel.cs
public double TotalSelectedSales
{
get
{
return this.AllCustomers.Sum(
custVM => custVM.IsSelected ? custVM.TotalSales : 0.0);
}
}

void OnCustomerViewModelPropertyChanged(object sender,
PropertyChangedEventArgs e)
{
string IsSelected = "IsSelected";

// When a customer is selected or unselected, we must let the
// world know that the TotalSelectedSales property has changed,
// so that it will be queried again for a new value.
if (e.PropertyName == IsSelected)
this.OnPropertyChanged("TotalSelectedSales");
}


这样提供一个思路……

当然你需要在AllCustomersViewModel构造函数里初始化如下代码:

foreach (CustomerViewModel cvm in all)
cvm.PropertyChanged += this.OnCustomerViewModelPropertyChanged;


也就是说在AllCustomersViewModel里侦测CustomerViewModel的IsSelected(已经与ListViewItem.IsSelected进行双向绑定)的变化……
沝林 2011-07-02
  • 打赏
  • 举报
回复
其实单选多选都可以不用ICollectionView的

单选,很简单,将SelectedItem与ViewModel的属性进行双向绑定就OK了

多选,由于ListView的SelectedItems不能进行绑定,需要将ListView的SelectionChanged事件转换成命令绑定到ViewModel,同时将SelectedItems传递到ViewModel层

示例:
首先添加程序集引用System.Windows.Interactivity.dll

xaml
添加命名空间引用


<UserControl ...
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
/>


<ListView ItemsSource="{Binding Source={StaticResource CustomerGroups}}"
x:Name="dataGrid1" >
<ListView.GroupStyle>
<StaticResourceExtension ResourceKey="CustomerGroupStyle" />
</ListView.GroupStyle>
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn DisplayMemberBinding="{Binding DisplayName,Mode=OneWay}" Header="Name" />
<GridViewColumn DisplayMemberBinding="{Binding Email,Mode=OneWay}" Header="E-Mail" />
<GridViewColumn DisplayMemberBinding="{Binding TotalSales,Mode=OneWay, ConverterCulture=zh-CN, StringFormat=c}" Header="Total Sales" />
</GridView.Columns>
</GridView>
</ListView.View>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelectionChangeCommand}" CommandParameter="{Binding SelectedItems,ElementName=dataGrid1}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListView>


viewmodel:

public RelayCommand<IList> SelectionChangeCommand
{
get
{
return new RelayCommand<IList>
(
(selectedItems) =>
{
TotalSelectedSales = 0;

CustomerViewModel firstCustomer = null;

if (selectedItems.Count > 0)
firstCustomer = selectedItems[0] as CustomerViewModel;

// 将选中第一条CustomerViewModel传递到主页面
selectedCustomer = firstCustomer;

// 更新按钮的可执行状态
EditCustomerCommand.RaiseCanExecuteChanged();
DeleteCustomerCommand.RaiseCanExecuteChanged();

foreach (dynamic Item in selectedItems)
TotalSelectedSales += Item.TotalSales;

RaisePropertyChanged("TotalSelectedSales");
}
);
}
}


示例代码可以在这里下载,里面有mvvm中ListView多选的代码:

http://download.csdn.net/source/3287687
mdh524 2011-07-01
  • 打赏
  • 举报
回复
汗。。。居然没人。。

8,756

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧