我的静态模型/属性未绑定到我的UI

And*_*son 2 data-binding wpf

WPF的新手。

我正在尝试将模型绑定到UI。因此,当在用户操作期间更改属性时,我希望该字段在UI上发生的任何地方进行更新。

这是我的模型:

namespace WpfApplication1
{
    public class model2
    {
        private static string myField2;

        public static string MyField2
        {

            get { return myField2; }
            set { myField2 = value; }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的标记:

<Window x:Class="WpfApplication1.MainWindow"     
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"       
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:model2 x:Key="mymodel"/>
    </Window.Resources>
    <Grid>
        <StackPanel Orientation="Vertical">
            <TextBlock Text="{Binding Source={StaticResource ResourceKey=mymodel}, Path=MyField2}"></TextBlock>     
            <Button Content="static test!" Click="Button_Click_1" />
        </StackPanel>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

我后面的代码:

using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            model2.MyField2 = "static!";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

UI上的字段不变吗?

nko*_*hvt 5

您需要通知对UI的更改,以便可以使用新值进行更新。

对于您的情况,您想通知静态属性更改,因此您需要一个静态事件。问题在于INotifyPropertyChanged接口需要一个成员事件,因此您将无法采用这种方式。

最好的选择是实现Singleton模式:

namespace WpfApplication1
{
    public class model2 : INotifyPropertyChanged
    {
        //private ctor so you need to use the Instance prop
        private model2() {}

        private string myField2;

        public string MyField2
        {

            get { return myField2; }
            set { 
                myField2 = value; 
                OnPropertyChanged("MyField2");
            }
        }

        private static model2 _instance;

        public static model2 Instance {
            get {return _instance ?? (_instance = new model2();)}
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName) {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后使您的属性成为成员属性并像这样进行绑定:

<Window x:Class="WpfApplication1.MainWindow"     
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"       
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:model2 x:Key="mymodel"/>
    </Window.Resources>
    <Grid>
        <StackPanel Orientation="Vertical">
            <TextBlock Text="{Binding Source={x:Static local:model2.Instance}, Path=MyField2}"/>  
            <Button Content="static test!" Click="Button_Click_1" />
        </StackPanel>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

后面的代码:

using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            model2.Instance.MyField2 = "static!";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)