8,734
社区成员
发帖
与我相关
我的任务
分享
<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");
}
foreach (CustomerViewModel cvm in all)
cvm.PropertyChanged += this.OnCustomerViewModelPropertyChanged;
<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>
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");
}
);
}
}