0
votes

I have set up a TreeView with HierarchicalDataTemplate. I want to set the IsSelected property of a TreeViewItem by Binding so in my ViewModel I have a Property "ObjectToSelectInTreeView" which holds the object.

In the TreeView I have a Style with multibinding

<Style TargetType="TreeViewItem">
  <Setter Property="IsSelected">
    <Setter.Value>
      <MultiBinding Converter="{StaticResource IsSelectedConverter}" Mode="OneWay">
        <Binding ElementName="TreeViewControl" Path="DataContext.DocumentToSelectInTree"
          UpdateSourceTrigger="PropertyChanged"></Binding>
        <Binding Path="."></Binding>
      </MultiBinding>
    </Setter.Value>
  </Setter>
  <Setter Property="Foreground" Value="Black"/>
  <Style.Triggers>
    <DataTrigger Binding="{Binding IsDeleted}" Value="True">
      <Setter Property="Foreground" Value="LightGray"/>
    </DataTrigger>
  </Style.Triggers>
</Style>

And the Converter:

public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
 {
   ModelBase itemForComparison;
   if (values[0] != null)
   {
     if (values[1] is ObservableDocument)
       itemForComparison = ((ObservableDocument)values[1]).Document;
     else
       itemForComparison = (ModelBase)values[1];

     if (values[0] == itemForComparison)
       return true;
   }
 return false;
}

First time all works. But when I manually select all items in TreeView and then set the ObjectToSelectInTreeView the converter will never run again and so no object is selected.

Thanks for every help!

1

1 Answers

0
votes

DataContext.DocumentToSelectInTree property changed is going to trigger the calculation of IsSelectedConverter converter.

I hope you might have implemented INotifyPropertyChanged interface for your ViewModel. Write the ItemSelectedCommand for the TreeView if you have not written already, and trigger the PropertyChanged Notification for DocumentToSelectInTree property whenever the ItemSelected is executed