【问题标题】:Two way Binding in WPF does not work with static membersWPF 中的两种方式绑定不适用于静态成员
【发布时间】:2012-04-18 10:21:52
【问题描述】:

Matt Hamilton 告诉我一个关于 WPF 的有趣事实:在 4.5 版本中可以使用静态变量以双向模式绑定。 不幸的是,V4.5 仍处于测试阶段,我决定更改我的代码以使我的应用程序最终运行正确。

但是 - 我仍然有类似的问题,我们开始吧:

我有一个非常简单的类“RecallConnectionSettings”。这个类的成员应该可以从代码中的任何地方访问,所以我决定将它们设为静态(像这样):

public class RecallConnectionSettings
    {
            private static string Server {get;set;} 
    }

如您所见:只有一个变量“服务器”。 现在我想要将 2WayMode 从 TextBox 文本属性绑定到该“服务器”值。

所以我尝试了这个:

<UserControl....>
    <UserControl.Resources>
            <local:RecallConnectionSettings x:Key="recallConf"/>
    </UserControl.Resources>
    <TextBox Text="{Binding Source={StaticResource recallConf}, Path=Server,  
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ... Name="txtServerAdress" />
</UserControl>

当我更改文本框中的值时,这很有效 - 但不是从另一边。 如果我(手动)更改“服务器”值,我的文本框中的文本属性将不会更新。

当然不是——我现在知道我必须在我的 RecallConnectionSettings 类中实现 INotifyProperty。 然后它看起来像这样:

 public class RecallConnectionSettings : INotifyPropertyChanged
    {
    public  event PropertyChangedEventHandler PropertyChanged;
    private static string s_server; 

    public static string Server
            {
                get { return s_server; }
                set
                {
                    s_server = value;
                    OnPropertyChanged("Server");
                }
            }

public static event PropertyChangedEventHandler PropertyChanged;



protected static void OnPropertyChanged(string name)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(name));
                }
            }
    }

好吧 - 这也行不通。因为只有静态方法,所以不能用类实例来调用事件:

PropertyChanged(this, new PropertyChangedEventArgs(name));

那么 - 现在该怎么办? 我考虑过使用单例,所以我这样做了:

public class RecallConnectionSettings : INotifyPropertyChanged
    {
        private static RecallConnectionSettings instance;

        private RecallConnectionSettings(){}

        public static RecallConnectionSettings Instance
        {
            get
            {
                if(instance == null)
                {
                    instance = new RecallConnectionSettings();
                }
                return instance;
            }
        }
// ... here comes the other stuff
}

为了让它工作,我还必须准备我的 UserControl,所以我这样做了:

...    
<UserControl.DataContext>
            <local:RecallConnectionSettings/>
</UserControl.DataContext>
...

此时无需继续尝试,因为要这样做,默认构造函数必须是公共的。

无论我在做什么:它都不起作用。 在我看来,我仍然不明白它是如何工作的 - 你会这么好心并告诉我诀窍吗?

【问题讨论】:

  • 你不应该从 INotifyPropertyChanged 派生吗?
  • VS 中的“输出-调试”有什么显示吗?您是否在绑定中激活了调试?就像@stijn 说的,你需要从 INotifyPropertyChanged 派生
  • 该死-你是对的..我会再试一次。谢谢。
  • 这已在 WPF 4.5 中修复:msdn.microsoft.com/en-us/library/…

标签: wpf binding staticresource two-way-binding


【解决方案1】:

保留单例解决方案并替换它:

...    
<UserControl>
    <UserControl.DataContext>
        <local:RecallConnectionSettings/>
    </UserControl.DataContext>
    ...
</UserControl>
...

通过这个:

...    
<UserControl DataContext="{x:Static local:RecallConnectionSettings.Instance}">
   ...
</UserControl>
...

【讨论】:

    【解决方案2】:

    哇- 谢谢 Nicolas,这行得通!

    对于其他读者 - 这是您现在必须为文本框编写的代码:

    <TextBox Text="{Binding Path=Server, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  Name="txtServerAdresse"/>
    

    【讨论】:

      猜你喜欢
      • 2015-05-29
      • 1970-01-01
      • 2010-12-13
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      相关资源
      最近更新 更多