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>
This is the correct one :
<Binding XPath="Contact/ContactMethods/ContactMethod[@Selected='True']"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With