1
votes

I want to bind a ComboBox to a XElement from a XDocument. The twoway-Binding works on a Textbox but not on the ComboBox:

xaml:

<Window x:Class="ComboBoxBindingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="31,38,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" SelectedValue="{Binding Path=Element[step].Element[schema].Value}" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="31,117,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="{Binding Path=Element[step].Element[schema].Value}"/>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="31,214,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
    </Grid>
</Window>

cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        comboBox1.Items.Add(new Tuple<string, string>("1", "Wert 1"));
        comboBox1.Items.Add(new Tuple<string, string>("2", "Wert 2"));
        comboBox1.Items.Add(new Tuple<string, string>("3", "Wert 3"));
        comboBox1.Items.Add(new Tuple<string, string>("4", "Wert 4"));
        comboBox1.Items.Add(new Tuple<string, string>("5", "Wert 5"));
        comboBox1.Items.Add(new Tuple<string, string>("6", "Wert 6"));
        comboBox1.Items.Add(new Tuple<string, string>("7", "Wert 7"));

        comboBox1.SelectedValuePath = "Item1";
        comboBox1.DisplayMemberPath = "Item2";

        XDocument doc = XDocument.Parse("<dir><step><schema>2</schema></step></dir>");
        DataContext = doc.Element("dir");
    }
}

Changing the text in the textbox to another number from 1 to 7 results in an update of the comboBox, but not vice versa. The Value in the XML is not changed when the comboBox selection is changed.

What is wrong in the ComboBox Binding?

Thanks

2
Is it really necessary to use the SelectedValue? Can you use SelectedItem instead? It seems, the SelectedValue does not work properly: stackoverflow.com/questions/247413/…Nico Schertler
I have only the value of the selected item in the xml (as seen in the example code), so I think I have to use the property SelectedValue, as I don't have the item.Roland Bär

2 Answers

0
votes

try this

<Window x:Class="ComboBoxBindingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="31,38,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120"  />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="31,117,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="{Binding ElementName=textBox2, Path=Text}" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="31,214,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="{Binding ElementName=textBox1, Path=Text}" />
    </Grid>
</Window>


public MainWindow()
{
    InitializeComponent();
    comboBox1.Items.Add(new Tuple<string, string>("1", "Wert 1"));
    comboBox1.Items.Add(new Tuple<string, string>("2", "Wert 2"));
    comboBox1.Items.Add(new Tuple<string, string>("3", "Wert 3"));
    comboBox1.Items.Add(new Tuple<string, string>("4", "Wert 4"));
    comboBox1.Items.Add(new Tuple<string, string>("5", "Wert 5"));
    comboBox1.Items.Add(new Tuple<string, string>("6", "Wert 6"));
    comboBox1.Items.Add(new Tuple<string, string>("7", "Wert 7"));

    comboBox1.SelectedValuePath = "Item1";
    comboBox1.DisplayMemberPath = "Item2";

    Binding binding = new Binding("Text");
    binding.Mode = BindingMode.TwoWay;
    binding.Source = textBox1;
   comboBox1.SetBinding(ComboBox.SelectedValueProperty, binding);
}
0
votes

It is a know Bug by Microsoft, a Hotfix is available: http://support.microsoft.com/kb/2328886

This Bug happens in the combination of binding Selector.SelectedValue (and also SelectedItem and SelectedIndex; Selector is the Base Class of ComboBox and also ListBox) with a XElement or XAttribute under .NET 4.0.