C# 使用XElement的组合框双向绑定
我想将组合框绑定到XDocument中的XElement。双向绑定适用于文本框,但不适用于组合框: xaml:C# 使用XElement的组合框双向绑定,c#,wpf,xml,binding,combobox,C#,Wpf,Xml,Binding,Combobox,我想将组合框绑定到XDocument中的XElement。双向绑定适用于文本框,但不适用于组合框: xaml: 政务司司长: 公共部分类主窗口:窗口 { 公共主窗口() { 初始化组件(); comboBox1.Items.Add(新元组(“1”,“wert1”)); comboBox1.Items.Add(新元组(“2”,“wert2”); comboBox1.Items.Add(新元组(“3”,“wert3”)); comboBox1.Items.Add(新元组(“4”,“Wert 4”
政务司司长:
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
comboBox1.Items.Add(新元组(“1”,“wert1”));
comboBox1.Items.Add(新元组(“2”,“wert2”);
comboBox1.Items.Add(新元组(“3”,“wert3”));
comboBox1.Items.Add(新元组(“4”,“Wert 4”);
comboBox1.Items.Add(新元组(“5”,“Wert 5”));
comboBox1.Items.Add(新元组(“6”,“Wert 6”));
comboBox1.Items.Add(新元组(“7”,“Wert 7”));
comboBox1.SelectedValuePath=“Item1”;
comboBox1.DisplayMemberPath=“Item2”;
XDocument doc=XDocument.Parse(“2”);
DataContext=doc.Element(“dir”);
}
}
将文本框中的文本更改为另一个从1到7的数字会导致组合框的更新,反之亦然。更改组合框选择时,XML中的值不会更改
组合框绑定有什么问题
谢谢你试试这个
<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);
}
公共主窗口()
{
初始化组件();
comboBox1.Items.Add(新元组(“1”,“wert1”));
comboBox1.Items.Add(新元组(“2”,“wert2”);
comboBox1.Items.Add(新元组(“3”,“wert3”));
comboBox1.Items.Add(新元组(“4”,“Wert 4”);
comboBox1.Items.Add(新元组(“5”,“Wert 5”));
comboBox1.Items.Add(新元组(“6”,“Wert 6”));
comboBox1.Items.Add(新元组(“7”,“Wert 7”));
comboBox1.SelectedValuePath=“Item1”;
comboBox1.DisplayMemberPath=“Item2”;
绑定=新绑定(“文本”);
binding.Mode=BindingMode.TwoWay;
binding.Source=textBox1;
ComboBox 1.设置绑定(ComboBox.SelectedValueProperty,绑定);
}
这是Microsoft的一个已知错误,可提供修补程序:
此错误发生在绑定Selector.SelectedValue(以及SelectedItem和SelectedIndex;Selector是ComboBox和ListBox的基类)与.NET 4.0下的XElement或XAttribute的组合中。是否确实需要使用SelectedValue?您可以改用SelectedItem吗?SelectedValue似乎无法正常工作:我在xml中只有所选项的值(如示例代码所示),因此我认为我必须使用SelectedValue属性,因为我没有该项。感谢您的解决方案,但您没有抓住要点。问题在于与XElement的绑定,您完全放弃了该绑定。
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");
}
}
<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);
}