WPF:如何从我的listview项目单击中的MouseLeftButtonUp事件绑定m对象属性
我有一个WPF:如何从我的listview项目单击中的MouseLeftButtonUp事件绑定m对象属性,wpf,listview,mouseleftbuttonup,Wpf,Listview,Mouseleftbuttonup,我有一个对象: public class Test : INotifyPropertyChanged { public string Name { get; set; } private bool isSelected; public bool IsSelected { get { return isSelected; } set {
对象
:
public class Test : INotifyPropertyChanged
{
public string Name { get; set; }
private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set
{
isSelected = value;
NotifyPropertyChanged("IsSelected");
}
}
public Test(string name, string path, bool selected)
{
Name = name;
Path = path;
isSelected = selected;
}
public override string ToString()
{
return Name;
}
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
因此,我将列表视图
绑定到我的对象
(测试
),当用户单击列表视图项
时,我想将我的IsSelected
属性从真
更改为假
(或反之亦然…)
和MouseLeftButtonUp
:
<ListView Name="listViewTests" ItemsSource="{Binding Tests}">
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding MouseLeftButtonUpCommand}"
CommandParameter="{Binding ElementName=listViewTests, Path=SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListView>
因此,与其在这个
执行方法中更改我的对象属性
,我想知道如何在XAML
中做到这一点,您可以设置ItemContainerStyle
并将ListBoxItem.IsSelected
属性绑定到您的数据模型测试.IsSelected
:
<ListView ItemsSource="{Binding Tests}">
<ListView.ItemContainerStyle>
<!-- The DataContext of this Style is the ListBoxItem.Content (a 'Test' instance) -->
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
您的意思是要反转视觉状态:如果选择了项目容器,则项目未被选中?这没有意义,这就是为什么我要问我是否理解正确。当使用CallerMemberNameAttribute
修饰方法参数时,不必将显式值(调用方的成员名称)传递给方法。这就是这个属性的全部目的。调用NotifyPropertyChanged()
就足够了。
<ListView ItemsSource="{Binding Tests}">
<ListView.ItemContainerStyle>
<!-- The DataContext of this Style is the ListBoxItem.Content (a 'Test' instance) -->
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</ListView.ItemContainerStyle>
</ListView>