-1

So I have this 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;
    }

So I have ListView bind with my object (Test) and when the user click on ListViewItem I want to change my IsSelected property from true to false (or Or vice versa...)

And 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>

My Execute Command:

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

            }
    }

So instead of change my object property inside this Execute method i wonder how to do that in XAML

Dana Yeger
  • 617
  • 3
  • 9
  • 26
  • Do you mean to invert the visual state: when item container is selected then the item is not? It doesn't make sense, that's why I'm asking if I understood you correctly. – BionicCode Sep 28 '20 at 12:23
  • When you use `CallerMemberNameAttribute` to decorate a method parameter, you don't have to pass the explicit value (the caller's member name) to the method. That's the whole purpose of this attribute. Invoking `NotifyPropertyChanged()` is sufficient. – BionicCode Sep 28 '20 at 12:26

1 Answers1

1

You can setup the ItemContainerStyle and bind the ListBoxItem.IsSelected property to your data model 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>
BionicCode
  • 1
  • 4
  • 28
  • 44