1、例TextBox的text关联类的属性
1.1、类的创建
class TestViewMode : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } public string show;//显示 public string Show { get { return show; } set { show = value; PropertyChanged(this, new PropertyChangedEventArgs("Show")); } }
}
1.2、XAML代码
<TextBox x:Name="textBox" Text="{Binding Path=Show, Mode=TwoWay}" />
1.3、关联上下文
public partial class MainWindow : Window { TestViewMode vm =new TestViewMode(); public MainWindow() { this.DataContext = vm; } }
2、类中静态变量在关联主窗口中的TextBox、在另外的页面中调用这个静态变量
2.1、类的创建
class TestViewMode : INotifyPropertyChanged { // 定义静态属性值变化事件 public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged; private static void OnStaticPropertyChanged([CallerMemberName] string propertyName = null) { OnStaticPropertyChanged(new PropertyChangedEventArgs(propertyName)); } private static void OnStaticPropertyChanged(PropertyChangedEventArgs e) { StaticPropertyChanged?.Invoke(null, e); } private static string property1; // 定义静态属性 public static string Property1 { get => property1; set { property1 = value; OnStaticPropertyChanged(); } } }
2.2、XAML代码
<TextBox x:Name="textBox" Text="{Binding Path=Property1, Mode=TwoWay}" />
2.3、关联上下文
public partial class MainWindow : Window { TestViewMode vm =new TestViewMode(); public MainWindow() { this.DataContext = vm; } }
2.4 在Page页面直接调用
TestViewMode.Property1 = "xxxxxx";
标签:string,bind,PropertyChangedEventArgs,TestViewMode,static,使用,new,WPF,public From: https://www.cnblogs.com/suwencjp/p/17934050.html