Change XPath dynamic binding in WPF

I bound my xml to a TreeView and bound my selected TreeViewItem to a TextBox. Now I need two TextBox. The first should turn on when TreeViewItem is selected and should change the XPath for TextBox-Content to "@name" if the element is the else category for "./title". And the second should only activate if the selected item is a map.

Is this only possible with wpf? And How?

TreeView Output:

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 (as of now):

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

Edit:

a tried this for the second case, but it does not include the text box:

(Only Include a text field if the item belongs to a type card)

<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>
+3
source share
1 answer

Hope I got it right, then this should work:

<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> 
+1

All Articles