ComboBox twoway与XElement绑定

时间:2012-07-10 16:24:27

标签: c# wpf xml binding combobox

我想将一个ComboBox绑定到XDocument的XElement。 twoway-Binding适用于文本框但不适用于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");
    }
}

将文本框中的文本更改为1到7之间的另一个数字会导致更新comboBox,但反之则不然。更改组合框选择时,不会更改XML中的值。

ComboBox绑定有什么问题?

由于

2 个答案:

答案 0 :(得分:0)

试试这个

<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);
}

答案 1 :(得分:0)

这是Microsoft的一个已知错误,可以使用一个Hotfix: http://support.microsoft.com/kb/2328886

此错误发生在绑定Selector.SelectedValue(以及SelectedItem和SelectedIndex; Selector是ComboBox的基类以及ListBox)的组合中,在.NET 4.0下具有XElement或XAttribute。