Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter in WPF XPath Expression

I have a ComboBox which lists the contact methods shown below. The ComboBox displays the correct values therefore the ItemSource Binding is working.

What i am trying to archive is that to display the selected contact method on application startup. I tried to bind the selected value to the ComboBox.Text attribute but i can't figure out how to set the filter.

This is my input data:

<Contact ShowsInterest="true">
  <Name>Tester</Name>
  <Lastname>Test</Lastname>
  <ContactMethods>
    <ContactMethod Selected="False">Phone</ContactMethod>
    <ContactMethod Selected="False">Email</ContactMethod>
    <ContactMethod Selected="True">Letter</ContactMethod>
    <ContactMethod Selected="False">Mobile</ContactMethod>
  </ContactMethods>
</Contact>

This is my ComboBox:

<ComboBox Name="combobox1" 
          ItemsSource="{Binding XPath=Contact/ContactMethods//*}" 
          Width="100" Height="25">
    <ComboBox.Text>
        <Binding XPath="Contact/ContactMethods//*[@Selected='true']"/>
    </ComboBox.Text>
</ComboBox>

The XPath Expression should do the following: Display the Element under Contact/ContactMethods/ where selected equals true.

EDIT: Even setting the Text Property directly won't work.

<ComboBox Name="combobox1" 
          ItemsSource="{Binding XPath=Contact/ContactMethods//*}" 
          Width="100" Height="25">
    <ComboBox.Text>
        Phone
    </ComboBox.Text>
</ComboBox>

I guess i have to use the SelectedValue Property:

<ComboBox Name="combobox1" 
          ItemsSource="{Binding XPath=Contact/ContactMethods//*}" 
          Width="100" Height="25">
    <ComboBox.SelectedValue>
        Phone
    </ComboBox.SelectedValue>
</ComboBox>

EDIT2: This is the working solution, thanks to MikroDel

<ComboBox Name="combobox1" 
            ItemsSource="{Binding XPath=Contact/ContactMethods//*}" 
            Width="100" Height="25">
    <ComboBox.SelectedValue>
        <Binding XPath="Contact/ContactMethods/ContactMethod[@Selected='True']"/>
    </ComboBox.SelectedValue>
</ComboBox>
like image 898
Joel Avatar asked Nov 26 '25 07:11

Joel


1 Answers

This is the correct one :

   <Binding XPath="Contact/ContactMethods/ContactMethod[@Selected='True']"/>
like image 191
MikroDel Avatar answered Nov 27 '25 23:11

MikroDel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!