.NET 4.5 中组合框与静态属性的双向绑定 [英] TwoWay Binding of a ComboBox to a static property in .NET 4.5

查看:30
本文介绍了.NET 4.5 中组合框与静态属性的双向绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我刚刚发布了这个问题,但我想我没有做错任何事.我的代码(以及所有答案的代码)是正确的,但是我的开发机器使用 .NET4.5 运行,这显然在组合框的绑定方面存在问题...

Well, i just posted this question, but i figured that i am not doing anything wrong. My code (and the code of all of the answers) is correct, but my dev-maschine runs with .NET4.5 which apparently has a problem with the binding of the combobox...

所以这里有一个新版本的问题:如何将组合框的 SelectedItem 双向绑定到 .NET4.5 中的静态属性?

So here a new version of the question: How to two-way bind a combobox's SelectedItem to a static Property in .NET4.5?

以下代码片段适用于 .net4,但不适用于 .NET4.5.在 4.5 中,只是所选值不会传播回我的静态属性.

The following code snippets work in .net4 but not in .NET4.5. In 4.5 it is just that the selected value is not propagated back into my static property.

我的视图模型:

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>

有没有人知道如何在 .NET4.5 中实现 ComboBox 的 SelectedItem 到静态属性的这种双向绑定?

Does anybody have an idea how to achieve this two-way binding of a ComboBox's SelectedItem to a static Property in .NET4.5?

推荐答案

请参考我在 Microsoft Connect 上的报告.每个 Selector 控件都受到此问题的感染.

Please refer to my report on Microsoft Connect. Every Selector control is infected by this issue.

最后,几周前微软发布了一个适当的补丁,该补丁已经通过 Windows 更新分发.请参阅知识库 KB2805222(WPF - 问题 7)

Finally, a few weeks ago Microsoft released an appropriate patch which has already been distributed through Windows Update. see Knowledge Base KB2805222 (WPF - Issue 7)

WPF - 第 7 期:假设您使用包含静态属性的绑定路径更改选择器属性(例如 SelectedItem 属性)或 ComboBox.Text 属性.在这种情况下,绑定不会对更改做出反应.具体来说,新值不会写入数据项.

WPF - Issue 7: Assume that you change a selector property (such as, the SelectedItem property) or ComboBox.Text property by using a binding path that contains a static property. In this situation, the binding does not react to the changes. Specifically, the new value is not written to the data item.

这篇关于.NET 4.5 中组合框与静态属性的双向绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆