views:

57

answers:

2

I'm using a TreeView inside a Combobox control (from here). I have a ViewModel object that is the DataContext of my window.

What I would like to have happen is when the selected item in the treeview/combobox changes I want a property on my ViewModel to be updated with that selected item. Ideally I'd like to be able to do this completely in xaml but I can't quite figure out how to do it.

The control has a "SelectedTreeViewItem" dependency property so basically I want to bind that property to a dependency property on my ViewModel object, but I don't know what exactly I need to do it (Trigger? EventTrigger?). The binding only has to be one way as I just want my view model's property to reflect what's currently selected in the control; I don't need to change the control's currectly selected item from my view model. I'm still pretty new to WPF.

Here's the code for my control attempting to bind the SelectedTreeViewItem property to a property on my view model. It doesn't work, the property on my view model is always null.

    <local:TreeViewCombo
        x:Name="encounterCodeSelector"
        ItemsSource="{Binding Path=EncounterCodes}"
        ItemTemplate="{StaticResource EncounterCodesTemplate}"             
        Style="{StaticResource TreeViewInComboBox}"
        SelectedTreeViewItem="{Binding Path=SelectedEncounterCode, Mode=OneWay}"
        Canvas.Left="171" Canvas.Top="377" Width="456">
    </local:TreeViewCombo>

Edit:

Changing the mode from "OneWay" to "OneWayToSource" worked.

A: 

Just bind the SelectedTreeViewItem to your ViewModel's property, using BindingMode=OneWay. A standard binding should work fine for this.

Reed Copsey
What's a "standard binding"? The <binding> tag? I've tried that and vs.net complains so either I'm putting it in the wrong spot or using it wrong.
Joe
A: 

Changing the mode from "OneWay" to "OneWayToSource" worked.

    <local:TreeViewCombo
        x:Name="encounterCodeSelector"
        ItemsSource="{Binding Path=EncounterCodes}"
        ItemTemplate="{StaticResource EncounterCodesTemplate}"             
        Style="{StaticResource TreeViewInComboBox}"
        SelectedTreeViewItem="{Binding Path=SelectedEncounterCode, Mode=OneWayToSource}"
        Canvas.Left="171" Canvas.Top="377" Width="456">
    </local:TreeViewCombo>
Joe