在WPF中动态更改XPath-Binding

时间:2011-03-23 01:17:07

标签: wpf dynamic binding textbox xpath

我已将xml绑定到TreeView并将选定的TreeViewItem bindet为TextBox。 现在我需要两个TextBox。第一个应该在选择TreeViewItem时启用,如果该项是“./title”的其他类别,则必须将TextBox-Content的XPath更改为“@name”。 第二个应该只在所选项目是卡片时启用。

这只能用wpf吗?怎么样?

TreeView输出:

o Categoryname
    - something
    o SubCategory
        - something else
- text             

XML:

<root>
  <cards>
    <category name="Categoryname">
      <card>
        <title>something</title>
        <content>the content</content>
        ..
      </card>
      <category name="SubCategory">
        <card>
          <title>something else</title>
          <content>the content</content>
          ...
        </card>
      </category>
    </category>
    <card>
      <title>text</title>
      <content>the content</content>
      ..
    </card>
  </cards>
</root>

TextBox(就像现在一样):

<TextBox Name="textBoxTitel" 
         DataContext="{Binding ElementName=listViewCards, Path=SelectedItem}" 
         Text="{Binding XPath=./title, UpdateSourceTrigger=PropertyChanged}"  
         IsReadOnly="False">
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=listViewCards, Path=SelectedItem}" Value="{x:Null}">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

编辑:

尝试了第二种情况,但它没有启用文本框:

(仅当元素来自类型卡时才启用文本框)

<TextBox IsEnabled="False" DataContext="{Binding ElementName=treeView, Path=SelectedItem}">
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding LocalName}" Value="card">
                    <Setter Property="Text" Value="{Binding XPath=./question, UpdateSourceTrigger=PropertyChanged}"/>
                    <Setter Property="IsEnabled" Value="True"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

1 个答案:

答案 0 :(得分:1)

我希望我明白这一点,然后这应该有效:

<TextBox Name="textBoxTitel" 
         DataContext="{Binding ElementName=listViewCards, Path=SelectedItem}"
         IsReadOnly="False">
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding}" Value="{x:Null}">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding LocalName}" Value="category">
                    <Setter Property="Text" Value="{Binding XPath=@name, UpdateSourceTrigger=PropertyChanged}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding LocalName}" Value="card">
                    <Setter Property="Text" Value="{Binding XPath=./title, UpdateSourceTrigger=PropertyChanged}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox> 
<TextBox Name="secondTextBox" 
         DataContext="{Binding ElementName=listViewCards, Path=SelectedItem}"
         IsReadOnly="False">
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="IsEnabled" Value="False"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding LocalName}" Value="card">
                    <Setter Property="IsEnabled" Value="True"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>