TwoWay绑定一个ComboBox到一个静态属性

TwoWay绑定一个ComboBox到一个静态属性

问题描述:

------- EDIT ------

因此,我认为以及来自所有答案的代码段。感谢那。我的问题是,我的dev-maschine运行.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.

------- EDIT ------

首先,简单的例子如何双向绑定我的组合框到我的数据上下文:

First, the simple example how i two-way bind my combobox to my data context:

查看模型:

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



我不需要在代码中设置datacontext -behind,我知道,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>

初始值已正确绑定,但如果我选择另一个项目与组合框,它不会反映我的viewModel。我做错了什么?

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}"/>

显然我的绑定字符串是正确的,但是我使用combobox的方式似乎是错误的。我也试图绑定 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>

我已上传示例