开发者

Change XPath-Binding dynamicaly in WPF

开发者 https://www.devze.com 2023-02-18 22:10 出处:网络
I have binded my xml to a TreeView and bindet my selected TreeViewItem to a TextBox. Now i need two TextBox. The first should enable when a TreeViewItem is selected and must change the XPath for the T

I have binded my xml to a TreeView and bindet my selected TreeViewItem to a TextBox. Now i need two TextBox. The first should enable when a TreeViewItem is selected and must change the XPath for the TextBox-Content to "@name" if the item is a category else to "./title". And the second should only enable if the selected item is a card.

Is this possible only 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 things are now):

<TextBox Name="textBoxTitel" 
         DataContext="{Binding ElementName=listViewCards, Path=SelectedItem}" 
         Text="{Binding XPath=./title, UpdateSourceTrigger=PropertyChanged}"  
         IsReadOnly="False">
开发者_StackOverflow中文版    <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 don´t enable the textbox:

(only Enable the textbox if the element is from 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>


I hope I understood this 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> 
0

精彩评论

暂无评论...
验证码 换一张
取 消