在HierarchicalDataTemplate中使用XPath麻烦绑定CommandParameter

时间:2011-07-20 17:10:15

标签: wpf data-binding xpath treeview hierarchicaldatatemplate

我对WPF比较新,我遇到了一个小问题。使用HierarchicalDataTemplates,我已成功将XML绑定到TreeView控件。每个节点都正确呈现(包括SubstepTemplate中的Label)。

我甚至可以从SubstepTemplate中的Button获取我的ViewModel中的绑定命令,但前提是我输入CommandParameter的硬编码值(例如999)。绑定到XML中commandID属性的所有尝试都失败了。

这就是我现在所拥有的:

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代码段:

    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;
    }

在CommandParameter中需要使用哪个正确的Binding表达式才能使其工作? {绑定XPath = @ commandID}似乎不起作用,我不明白为什么不。

1 个答案:

答案 0 :(得分:0)

我发现了问题。当XPath用作CommandParameter绑定时,模板化的DelegateCommand接收System.Xml.XmlNode类型的参数,而不是字符串。

尝试获取命令数据类型时,我在命令中遇到异常

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

因为TypeConverter :: CanConvertFrom不知道如何处理System.Xml.XmlNode类型的参数。

我能够通过显式测试

来获取commandID值
    data is System.Xml.XmlNode

在GetCommandDataType(对象数据)中,然后返回.Value成员,即

        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);
        }