TreeView Sync to SelectedItem in View Model
Asked Answered
L

2

9

I have a ViewModel on top of a WPF TreeView control. I want the ViewModel to be able to set and read the SelectedItem from the TreeView. However, the SelectedItem property of the TreeView is not bindable.

I am able to set and get the selected item in the code behind (using the ItemContainerGenerator and TreeViewItem.IsSelected = true) but this leads to some ugly communication between the code behind and the ViewModel.

Does anyone have a clean solution for this?

Loan answered 12/11, 2009 at 16:29 Comment(2)
Did you ever resolve this? I have exactly this issue.Contessacontest
I think this question is duplicate of #1000540Capet
D
1

I can provide an example. What I do is setting the IsSelected property of a TreeViewItem (not the TreeView itself) in the view model, because you can bind to this.

In my view model I have a property ElementInViewModel which is a data structure that forms a tree itself. I use a HierarchicalDataTemplate in my Xaml to display it. The data object itself is of type YourDomainType and its child elements (of the same type) are in its ChildElements property.

In the view model, I set the IsExpanded and IsSelected property of my data class YourDomainType. Because of the style defined below, they will pass this setting to the TreeViewItem.

Does this work for you?

<UserControl>

    <UserControl.Resources>        
        <CollectionViewSource Source="{Binding Path=ElementInViewModel}" x:Key="Cvs">
        </CollectionViewSource>

        <HierarchicalDataTemplate DataType="{x:Type DomainModel:YourDomainType}"
                                  ItemsSource="{Binding Path=ChildElements}">
            <TextBlock Text="{Binding Path=Name}"/>            
        </HierarchicalDataTemplate>        

        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            </Setter>
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            </Setter>            
        </Style>

    </UserControl.Resources>


    <DockPanel>
        <TreeView ItemsSource="{Binding Source={StaticResource Cvs}}"/>
    </DockPanel>

</UserControl>
Disconnection answered 4/8, 2015 at 19:42 Comment(0)
I
0

You can use some kind of proxy class to bind SelectedItem property to In property and Out property bind to your ViewModel:

    public class Proxy : FrameworkElement
    {
    public static readonly DependencyProperty InProperty;
    public static readonly DependencyProperty OutProperty;

    public Proxy()
    {
        Visibility = Visibility.Collapsed;
    }

    static Proxy()
    {
        var inMetadata = new FrameworkPropertyMetadata(
            delegate(DependencyObject p, DependencyPropertyChangedEventArgs args)
                {
                    if (null != BindingOperations.GetBinding(p, OutProperty))
                    {
                        var proxy = p as Proxy;
                        if (proxy != null)
                            proxy.Out = args.NewValue;
                    }
                });

        inMetadata.BindsTwoWayByDefault = false;
        inMetadata.DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

        InProperty = DependencyProperty.Register("In",
                                                 typeof (object),
                                                 typeof (Proxy),
                                                 inMetadata);

        var outMetadata = new FrameworkPropertyMetadata(
            delegate(DependencyObject p, DependencyPropertyChangedEventArgs args)
                {
                    ValueSource source = DependencyPropertyHelper.GetValueSource(p, args.Property);

                    if (source.BaseValueSource != BaseValueSource.Local)
                    {
                        var proxy = p as Proxy;
                        if (proxy != null)
                        {
                            var expected = proxy.In;
                            if (!ReferenceEquals(args.NewValue, expected))
                            {
                                Dispatcher.CurrentDispatcher.BeginInvoke(
                                    DispatcherPriority.DataBind, new Action(delegate
                                                                                {
                                                                                    proxy.Out = proxy.In;
                                                                                }));
                            }
                        }
                    }
                });

        outMetadata.BindsTwoWayByDefault = true;
        outMetadata.DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

        OutProperty = DependencyProperty.Register("Out",
                                                  typeof (object),
                                                  typeof (Proxy),
                                                  outMetadata);
    }

    public object In
    {
        get { return GetValue(InProperty); }
        set { SetValue(InProperty, value); }
    }

    public object Out
    {
        get { return GetValue(OutProperty); }
        set { SetValue(OutProperty, value); }
    }
}
<Proxy In="{Binding ElementName=Tree, Path=SelectedItem}" Out="{Binding SelectedItem, UpdateSourceTrigger=PropertyChanged}"/>
<TreeView x:Name="Tree" ItemsSource="{Binding Path=Items}"/>
Illustration answered 27/12, 2012 at 11:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.