Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6212085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:27:02+00:00 2026-05-24T06:27:02+00:00

I am relatively new to WPF and I have run into a little problem.

  • 0

I am relatively new to WPF and I have run into a little problem. Using HierarchicalDataTemplates, I have successfully bound XML to a TreeView control. Each node renderers correctly (including the Label in the SubstepTemplate).

I can even get the bound Command in my ViewModel from the Button in the SubstepTemplate, but only if I enter a hardcoded value for the CommandParameter (e.g. 999). All attempts at binding to the commandID attribute in my XML have failed.

Here is what I have right now:

XML:

<root xmlns="">
  <step label="Step Label 1">
    <button label="Button Title 1A" commandID="701" />
    <button label="Button Title 1B" commandID="702" />
    <button label="Button Title 1C" commandID="703" />
  </step>
  <step label="Step Label 2">
    <button label="Button Title 2A" commandID="801" />
    <button label="Button Title 2B" commandID="802" />
  </step>
</root>

XAML:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SidePanel">

<HierarchicalDataTemplate
            x:Key="SubstepTemplate"
            DataType="button"
            ItemsSource="{Binding XPath=*}">
    <StackPanel Orientation="Horizontal">
        <Button Margin="2" Width="32" Height="32" Command="{Binding ElementName=MyTreeView, Path=DataContext.PluginCommand}" CommandParameter="{Binding XPath=@commandID}" />
        <Label VerticalAlignment="Center" Margin="8,0,0,0" Content="{Binding XPath=@label}" />
    </StackPanel>
</HierarchicalDataTemplate>

<HierarchicalDataTemplate
            x:Key="StepTemplate"
            DataType="step"
            ItemsSource="{Binding XPath=*}"
            ItemTemplate="{StaticResource SubstepTemplate}">
    <Expander Header="{Binding Mode=OneWay, XPath=@label}" HorizontalAlignment="Stretch">
    </Expander>
</HierarchicalDataTemplate>

<Style TargetType="{x:Type local:SidePanelControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:SidePanelControl}">
                <TreeView
                    Name="MyTreeView"
                    ItemsSource="{Binding XmlRoot}"
                    ItemTemplate="{StaticResource StepTemplate}"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}"
                    HorizontalAlignment="Stretch">
                </TreeView>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</ResourceDictionary>

ViewModel Snippet:

    public DelegateCommand<string> PluginCommand
    {
        get
        {
            if (pluginCommand == null)
            {
                pluginCommand = new DelegateCommand<string>(ExecPluginCommand, canExecPluginCommand);
            }
            return pluginCommand;
        }
    }

    public void ExecPluginCommand(string param)
    {
        LogMessage("In ExecPluginCommand, param = " + param);
    }

    public bool canExecPluginCommand(string param)
    {
        return true;
    }

What is the correct Binding expression that needs to go in the CommandParameter to get this to work? {Binding XPath=@commandID} doesn’t seem to work, and I do not understand why not.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:27:03+00:00Added an answer on May 24, 2026 at 6:27 am

    I found the problem. When an XPath is used as a CommandParameter binding, the templated DelegateCommand receives a parameter of type System.Xml.XmlNode, not string.

    I was getting an exception in my Command when attempting to get the command data type

        void ICommand.Execute(object parameter)
        {
            TParam value = GetCommandDataType(parameter);
            Execute(value);
        }
    

    because TypeConverter::CanConvertFrom did not know what to do with an argument of type System.Xml.XmlNode.

    I was able to get the commandID value by testing explicitly for

        data is System.Xml.XmlNode
    

    in GetCommandDataType(object data) and then returning the .Value member, i.e.

            else if (data is System.Xml.XmlNode)
            {
                System.Xml.XmlNode foo = (System.Xml.XmlNode)data;
                string fooVal = foo.Value;
                TypeConverter converter = TypeDescriptor.GetConverter(typeof(string));
                result = (TParam)converter.ConvertFrom(fooVal);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm relatively new to using WPF and the MVVM architecture. I have a question
I'm relatively new to VB.NET and WPF and I have a basic threading question.
I'm using Visual Studio 2010, c# WPF. I have created a MediaElement control during
I've been using Windows Forms for years, but I'm relatively new to WPF. I
I'm relatively new to OOD, C#, WPF, but trying to learn. So I have
I'm relatively new to working with XML and am working with some rather large
I'm relatively new to .Net 4 and I am creating my FIRST WPF application
Im using prism 4.0, wpf, I create RegionAdapter for devx DocumentGroup. I have a
Im relatively new to PHP but have realized it is a powerfull tool. So
I am using the new WPF toolkit's Chart to plot large data sets. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.