-->

组合框的双向绑定到一个静态属性(TwoWay Binding of a ComboBox to a

2019-06-26 06:10发布

- - - -编辑 - - -

所以,我想, 我的代码是正确的,那么从你的答案的代码片段 。 感谢那。 我的问题是我的dev-maschine运行.NET4.5其行为有所不同! 非常相同的程序(针对.NET4.0编译)运行在同一个.NET4.0正确maschine但不与.NET4.5一个maschine!

因此,这里是我的修订问题 。

- - - -编辑 - - -

首先,简单的例子,我怎么双向我组合框绑定到我的数据方面:

浏览模式:

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

这工作得很好,如果我选择蒙山组合框的项目,它被绑定到我的视图模型。

好了,现在我想我的视图模型静态但仍双向绑定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了,我知道,那XAML需要一个实例为双向绑定,所以我还是默认的构造函数。 我然后绑定的组合框

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

初始值是正确的约束,但如果我选择与组合框的另一项目它,它不会反映在我的视图模型。 我究竟做错了什么?

编辑:

如果我使用完全相同的绑定字符串文本框,并更改框中的文本,它反映在财产。

<TextBox Text="{Binding Source={StaticResource model}, Path=SelectedElement}"/>

所以,很显然我的绑定字符串是正确的,但我用的是组合框的方式似乎是错误的。 我也试图绑定SelectedValue ,而不是...没有任何变化。

Answer 1:

只是在一个示例项目检查,工作正常

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>

我已经上传样本 。



Answer 2:

你被迷惑实例和静态属性之间:你并不需要绑定一个静态对象。

<ComboBox
        ItemsSource="{x:Static me:MainWindowViewModel.MyElements}"
        SelectedItem="{x:Static me:MainWindowViewModel.SelectedElement}" />

你应该实现INotifyPropertyChanged不过。

绑定是有关解决从中要获取数据正确的实例。
如果没有实例的意义,没有必要结合。



文章来源: TwoWay Binding of a ComboBox to a static property