Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 数据绑定到静态单例_Wpf_Xaml_Data Binding - Fatal编程技术网

Wpf 数据绑定到静态单例

Wpf 数据绑定到静态单例,wpf,xaml,data-binding,Wpf,Xaml,Data Binding,我在绑定到WPF表单时遇到问题。我有自己的静态“设置”类(singleton),它实现PropertyChangedEventHandler并在更新属性时引发事件 singleton对象被添加到表单构造函数中的资源中,并且在表单初始化时正确读取属性,从而表明绑定是正确的 但是,WPF不会为PropertyChangedEventHandler注册任何事件处理程序,PropertyChanged始终为null。因此,事件永远不会引发,我的表单也永远不会更新(只需单击按钮即可更新) 我做错了什么 我

我在绑定到WPF表单时遇到问题。我有自己的静态“设置”类(singleton),它实现PropertyChangedEventHandler并在更新属性时引发事件

singleton对象被添加到表单构造函数中的资源中,并且在表单初始化时正确读取属性,从而表明绑定是正确的

但是,WPF不会为PropertyChangedEventHandler注册任何事件处理程序,PropertyChanged始终为null。因此,事件永远不会引发,我的表单也永远不会更新(只需单击按钮即可更新)

我做错了什么

我怀疑出于某种原因调用Resources.Add会阻止WPF注册自己的事件处理程序,但我不确定

我已经阅读了关于类似主题的多个SO问题,但最常见的两个问题是没有创建适当的单例(从而将另一个实例传递给xaml),或者没有实现INotifyPropertyChanged。这两件事我都做对了

预期行为:

namespace bindings
{
public sealed class Settings : INotifyPropertyChanged
{
    private static readonly Settings instance = new Settings();
    private Settings() 
    {
    }
    public static Settings Instance { get { return instance; } }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName = null)
    {
        // passing propertyName=null raises the event for all properties
        if (PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    private string textValue = "testOK";
    public static string TextValue
    {
        get { return Instance.textValue; }
        set { Instance.textValue = value; Instance.NotifyPropertyChanged(); }
    }
}
Settings.TextValue是我感兴趣的属性。在其setter中调用NotifyPropertyChanged,不幸的是,由于WPF未注册任何处理程序,因此无法引发此.PropertyChanged事件

单击MainWindow.Button1时,文本框的值应该从Settings.textBox(“testOK”)的初始值更改为“ButtonA OK”

代码如下:

设置。cs:

namespace bindings
{
public sealed class Settings : INotifyPropertyChanged
{
    private static readonly Settings instance = new Settings();
    private Settings() 
    {
    }
    public static Settings Instance { get { return instance; } }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName = null)
    {
        // passing propertyName=null raises the event for all properties
        if (PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    private string textValue = "testOK";
    public static string TextValue
    {
        get { return Instance.textValue; }
        set { Instance.textValue = value; Instance.NotifyPropertyChanged(); }
    }
}
MainWindow.xaml.cs

namespace bindings
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        Resources.Add("foobar", Settings.Instance);
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        int hash = Settings.Instance.GetHashCode();
        Settings.TextValue = "ButtonA OK";
    }
}
}
命名空间绑定
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
添加(“foobar”,Settings.Instance);
初始化组件();
}
已加载私有无效窗口(对象发送器、路由目标)
{
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
int hash=Settings.Instance.GetHashCode();
Settings.TextValue=“按钮确定”;
}
}
}
main window.xaml

<Window x:Class="bindings.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" WindowStyle="ToolWindow">
<Grid PresentationTraceSources.TraceLevel="High" DataContext="{StaticResource foobar}">
    <Button Content="ButtonA" Height="33" HorizontalAlignment="Left" Margin="76,243,0,0" Name="button1" VerticalAlignment="Top" Width="101" Click="button1_Click" />
    <TextBox Height="28" HorizontalAlignment="Left" Margin="182,180,0,0" Name="textBox1" VerticalAlignment="Top" Width="93" 
             Text="{Binding Path=TextValue, Mode=OneWay}" DataContext="{Binding}" PresentationTraceSources.TraceLevel="High"/>
</Grid>
</Window>


谢谢你的帮助

将您的示例完全复制到一个全新的VS项目中,所有这些都可以按照您所描述的那样工作。我认为你简化了你的bug,脱离了你的例子……事实上,我刚刚在一台不同的机器上重建了这个项目,一切正常。上一个仍然不起作用。。。不知道为什么。无论如何,我想问题解决了,谢谢!