0

所以我有这个object

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;
    }

所以我已经ListView绑定了我的object( Test) 并且当用户点击ListViewItem我想将我的IsSelected属性从更改truefalse(或反之亦然......)

并且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>

我的Execute命令:

    public void Execute(object parameter)
    {
        Test test = parameter as Test;
        if (test != null)
            {

            }
    }

property因此,与其在此方法中更改我的对象,Execute我想知道如何在XAML

4

1 回答 1

0

您可以设置ItemContainerStyle并将属性绑定ListBoxItem.IsSelected到您的数据模型Test.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>
于 2020-09-28T12:11:36.983 回答