【问题标题】:ComboBox twoway binding with XElement与 XElement 的 ComboBox 双向绑定
【发布时间】:2012-07-10 04:29:56
【问题描述】:

我想将 ComboBox 绑定到 XDocument 中的 XElement。双向绑定适用于文本框,但不适用于组合框:

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 的另一个数字会导致组合框更新,但反之则不然。更改组合框选择时,XML 中的值不会更改。

ComboBox 绑定有什么问题?

谢谢

【问题讨论】:

  • 真的有必要使用SelectedValue吗?您可以改用 SelectedItem 吗?看来,SelectedValue 不能正常工作:stackoverflow.com/questions/247413/…
  • 我只有xml中选中项的值(如示例代码所示),所以我认为我必须使用属性SelectedValue,因为我没有该项。

标签: c# wpf xml binding combobox


【解决方案1】:

这是 Microsoft 已知的错误,有可用的修补程序: http://support.microsoft.com/kb/2328886

此错误发生在 .NET 4.0 下将 Selector.SelectedValue(以及 SelectedItem 和 SelectedIndex;Selector 是 ComboBox 和 ListBox 的基类)与 XElement 或 XAttribute 结合使用时。

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

    • 感谢您的解决方案,但您错过了重点。问题是 与 XElement 的绑定,您完全放弃了它。
    猜你喜欢
    • 2011-09-06
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 2011-02-16
    • 2014-06-09
    • 2011-09-02
    • 1970-01-01
    相关资源
    最近更新 更多