ComboBox 到静态属性的双向绑定 [英] TwoWay Binding of a ComboBox to a static property
问题描述
-------编辑------
所以,我认为我的代码是正确的,您所有答案中的代码片段也是如此.感谢那.我的问题是我的开发机器运行 .NET4.5,它的行为不同!完全相同的程序(针对 .NET4.0 编译)在带有 .NET4.0 的机器上运行正确,但在带有 .NET4.5 的机器上运行不正确!
So, i figured that my code is correct and so are the code snippets from all of your answers. Thanks for that. My problem is that my dev-maschine runs .NET4.5 which behaves differently! The very same program (compiled against .NET4.0) runs correct on a maschine with .NET4.0 but not on a maschine with .NET4.5!
所以这是我修改后的 问题.
So here is my revised question.
-------编辑------
首先,我如何将组合框双向绑定到数据上下文的简单示例:
查看模型:
public class MainWindowViewModel
{
public List<String> MyElements { get; set; }
public string SelectedElement { get; set; }
public MainWindowViewModel()
{
MyElements = new List<string>() {"a", "b", "c"};
SelectedElement = "a";
}
}
和代码隐藏
private readonly MainWindowViewModel _viewModel = new MainWindowViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = _viewModel;
}
和我的 xaml
<ComboBox
ItemsSource="{Binding MyElements, Mode=OneWay}"
SelectedItem="{Binding SelectedElement}" />
这很好用,如果我选择了一个带有组合框的项目,它就会绑定到我的视图模型.
This works fine and if i select an item whith the combobox, it is bound to my view model.
好的,现在我想让我的 viewModel 静态但仍然双向绑定 selectedItem.我试试这个:
public class MainWindowViewModel
{
public static List<String> MyElements { get; set; }
public static string SelectedElement { get; set; }
static MainWindowViewModel()
{
MyElements = new List<string>() {"a", "b", "c"};
SelectedElement = "a";
}
}
我不再需要在代码隐藏中设置数据上下文,我知道,xaml 需要一个双向绑定实例,所以我仍然使用默认构造函数.然后我绑定组合框
I do not need to set the datacontext in the Code-behind anymore and i know, that xaml needs an instance for two-way binding, so i have still the default constructor. I then bind the combobox
<Window.Resources>
<me:MainWindowViewModel x:Key="model"/>
</Window.Resources>
<StackPanel>
<ComboBox
ItemsSource="{Binding Source={x:Static me:MainWindowViewModel.MyElements}, Mode=OneWay}"
SelectedItem="{Binding Source={StaticResource model}, Path=SelectedElement}" />
</StackPanel>
初始值已正确绑定,但如果我使用组合框选择另一个项目,它不会反映在我的视图模型中.我做错了什么?
The initial value is properly bound, but if i select another item with the combobox it it not reflected in my viewModel. What am i doing wrong?
如果我对 TextBox 使用完全相同的绑定字符串并更改框中的文本,它会反映在属性中.
If I use the exact same binding string for a TextBox and change the text in the box, it is reflected in the property.
<TextBox Text="{Binding Source={StaticResource model}, Path=SelectedElement}"/>
显然我的绑定字符串是正确的,但我使用组合框的方式似乎是错误的.我也尝试绑定 SelectedValue
代替......也没有改变.
So obviously my binding string is correct but the way i use the combobox seems to be wrong. I also tried to bind SelectedValue
instead... no change either.
推荐答案
仅在示例项目中检查,工作正常
Checked just in a sample project, works fine
public class ViewModel
{
static ViewModel()
{
Items=new ObservableCollection<string>();
SelectedItem = "222";
Items.Add("111");
Items.Add("222");
Items.Add("333");
Items.Add("444");
Items.Add("555");
}
private static string _selectedItem;
public static string SelectedItem
{
get { return _selectedItem; }
set { _selectedItem = value;
MessageBox.Show("Item " + value + " was selected");
}
}
private static ObservableCollection<string> _items;
public static ObservableCollection<string> Items
{
get { return _items; }
set { _items = value; }
}
}
和xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:my="clr-namespace:WpfApplication1"
Title="MainWindow" Height="200" Width="300">
<Grid>
<Grid.Resources>
<my:ViewModel x:Key="viewM"/>
</Grid.Resources>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="101,12,0,0" Name="comboBox1" VerticalAlignment="Top" Width="146"
ItemsSource="{Binding Source={x:Static my:ViewModel.Items}, Mode=OneWay}"
SelectedItem="{Binding Source={StaticResource viewM}, Path=SelectedItem}" />
</Grid>
</Window>
我已上传示例.
这篇关于ComboBox 到静态属性的双向绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!