TwoWay Binding of a ComboBox to a static property

Martin Booka Weser picture Martin Booka Weser · Jul 13, 2012 · Viewed 9.1k times · Source

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

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:

View model:

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

and code-behind

private readonly MainWindowViewModel _viewModel = new MainWindowViewModel();
public MainWindow()
{
    InitializeComponent();
    DataContext = _viewModel;
}

and my 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.

OK, now i want to make my viewModel static but still two-way bind the selectedItem. I try this:

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

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?

EDIT:

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

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.

Answer

Artiom picture Artiom · Jul 13, 2012

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

and 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>

I have uploaded sample.