【问题标题】:TwoWay Binding of a ComboBox to a static property组合框到静态属性的双向绑定
【发布时间】:2012-07-13 10:15:38
【问题描述】:

-------编辑------

所以,我认为我的代码是正确的,你所有答案中的代码 sn-ps 也是正确的。感谢那。我的问题是我的开发机器运行的 .NET4.5 行为不同!相同的程序(针对 .NET4.0 编译)在 .NET4.0 的机器上运行正确,但在 .NET4.5 的机器上运行不正确!

这是我修改后的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}" />

这很好用,如果我使用组合框选择一个项目,它就会绑定到我的视图模型。

好的,现在我想让我的 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 需要一个用于双向绑定的实例,所以我仍然有默认构造函数。然后我绑定组合框

<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 中。我做错了什么?

编辑:

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

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

显然我的绑定字符串是正确的,但我使用组合框的方式似乎是错误的。我还尝试绑定SelectedValue...也没有任何变化。

【问题讨论】:

  • 再次检查您的代码后,我不得不说它对我有用。我已修改public static string SelectedElement 以在调用setter 时写入跟踪输出,并且每次所选项目更改时都会调用它。您仍然可以按照@Baboon 的建议将ItemsSource 绑定替换为静态引用。
  • 呃,我已经安装了 .NET4.5。有没有可能这里变了?当然,我的财产中也有踪迹。为简单起见,这里省略了它。是的,我知道我通常也会实现 INotifyPropertyChanged 以将更改从属性绑定到 ComboBox。

标签: wpf xaml data-binding combobox two-way-binding


【解决方案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>

我已上传sample

【讨论】:

  • 刚刚检查过(不是您附加的 7z,而是复制了您的代码 sn-p):您的 MessageBox 在开始时只出现一次。如果我在复选框中选择另一个项目,则不会发生任何事情,并且 SelectedItem 属性不会更改。
  • 每次都为我工作。不知道有什么问题。从第一次开始工作
  • 你能上传示例项目看看那里发生了什么吗?
【解决方案2】:

您对实例和静态属性感到困惑:您不需要绑定静态对象。

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

不过你还是应该实现INotifyPropertyChanged

绑定是关于解析您要从中获取数据的正确实例。
如果没有实例的意义,就不需要绑定。

【讨论】:

  • ComboBox.SelectedItem 更改时,这是否会更新MainWindowViewModel.SelectedElement 属性?我认为这里的问题是绑定的双向方面。
  • 是的,应该。 SelectedItem 默认为“TwoWay”。
  • 但是你没有绑定。 SelectedItem 默认情况下肯定绑定双向,但是没有绑定就没有双向的东西。
猜你喜欢
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
  • 1970-01-01
  • 2013-07-22
  • 2019-07-28
  • 2010-10-30
  • 2014-09-10
  • 1970-01-01
相关资源
最近更新 更多