Question

How can I add WPF DelegateCommands to the items in a TreeView bound to an XmlDataProvider? I'm using the MVVM pattern and Composite WPF and I want the command to be called when the user double-clicks on an item in the TreeView.


I have a TreeView defined in XAML whose DataContext is set to the XmlDataProvider:

<TreeView
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    ItemsSource="{Binding XPath=/SomeTopElement/*}">
    <TreeView.Resources>
        <HierarchicalDataTemplate
            DataType="SomeElement"
            ItemsSource="{Binding XPath=child::*}">
            <TextBlock Text="{Binding XPath=@SomeAttribute}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

In other parts of the code I simply bind to a DelegateCommand in the ViewModel:

<MenuItem Command="{Binding NewCommand}" Header="_New" />

How can this be done with the above TreeView?

Was it helpful?

Solution

You should use the Attached Command Behavior pattern. This question answers a similar problem, but within a ListView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top