c#-WPF使用类和子类绑定到DataContext
2019-11-02 14:05:16 阅读:231 来源: 互联网
标签:data-binding wpf xaml c

我在wpf中玩数据绑定,遇到问题.这是我的代码:

MainWindow.xaml








MainWindow.xaml.cs

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MyClass myClass = new MyClass();
myClass.A = new MySubClass();
myClass.B = new MySubClass();
stackpanel.DataContext = myClass;
}
}
MyClass.cs

class MyClass : INotifyPropertyChanged
{
private MySubClass a;
public MySubClass A
{
get { return a; }
set
{
a = value;
OnPropertyChanged(“A”);
OnPropertyChanged(“C”);
}
}

private MySubClass b;
public MySubClass B 
    get { return b; }
        b = value;
        OnPropertyChanged("B");
        OnPropertyChanged("C");
public int C
    get { return A.Number + B.Number; }
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string p)
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(p));

}
MySubClass.cs

class MySubClass : INotifyPropertyChanged
{
private int number;
public int Number
{
get { return number; }
set
{
number = value;
OnPropertyChanged(“Number”);
}
}

public MySubClass()
    Number = 1;
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string p)
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(p));

}
现在,问题是在我运行应用程序后,绑定工作正常.另外,当我在文本框中更改值A.Number和B.Number时,它们也可以更新.但是,只有在应用启动时才更新MyClass.C中的变量C,之后才更新.我需要更改什么,以便当我更改A.Number或B.Number时C更新.谢谢.

解决方法:

在更新数据模型时,是否直接在MySubClass实例上更改Number或将新的子类分配给MyClass实例?即:

myClass.A.Number = 5; // will trigger OnPropertyChanged on MySubClass, but not on MyClass
不会触发A上的OnPropertyChanged(但当然会更新A.Number).
为此,您必须执行以下操作:

MySubClass v = new MySubClass()
v.Number = 5;
myClass.A = v; // will trigger OnPropertyChanged on MyClass
更新的答案.您可以执行此操作以捕获a和b中的任何属性更改.

public MyClass()
{
a.PropertyChanged += new PropertyChangedEventHandler(UpdateC);
b.PropertyChanged += new PropertyChangedEventHandler(UpdateC);
}

void UpdateC(object sender, PropertyChangedEventArgs e)
{
OnPropertyChanged(“C”);
}

标签:data-binding,wpf,xaml,c
来源: https://codeday.me/bug/20191102/1991948.html

先做个声明:这里绑定都在前台实现,至于后台怎么写,那比前台简单多了,但更常用的是xaml中绑定。我们分析下最简单的字符串绑定来弄清楚原理,其他的推就是。 数据绑定主要是要弄清楚两个东西,一个是源Source,一个是路径Path。 什么能够作为源Source呢: CLR型的单个对象 CLR型的集合对象 DataTable和DataView XML数据 Framewor...
获取或设置元素参与数据绑定时的数据上下文。数据上下文是一种概念,允许元素从父元素继承有关用于绑定的数据源以及绑定的其他特征(如路径)的信息。此依赖属性继承属性值。如果有子元素没有通过本地值或样式建立的DataContext的其他值,则属性系统会将该值设置为已分配此值的最近父元素的DataContext值。 在 XAML 中,DataContext通常设置为Binding声明。可以使用属性元素语法或特性语法。还可以使用代码设置DataConte... 其实事务在数据层、服务层、业务逻辑层多处地方都会使用到,在本篇文章将会为大家一一细说。 其中前面四节是事务的基础,后面的三节是事务的重点,对事务有基础的朋友可以跳过前面四节。 文章有错漏的地方欢迎各位点评。 一、事务的定义 二、事务管理器 三、在ADO.NET中实现事务 四、隐式事务 TransactionScope 五、在WCF中实现事务 六、嵌套式事务 ...
1)在FrameworkElement下有两个重要的属性。利用这两个属性,可以实现UI数据直接与后台的进行绑定2)前端UI界面属性值等动态与后台绑定的方式有多种,此处列出后台设置DataContext以及前端设置DataContext的两种方式,以及数据双向绑定的最佳实践纯前端的方式好处在于,编译一下,就可以在设计阶段看到效果。 1)先定义好我们的 2)在前端按照步骤引入 后台设置DataContext 在设计阶段是看不到效果的,但是灵活性更高,属性值不用写死 1)定义一个 2)在当前窗口加载时,进
随着技术的进步,跨平台开发已经成为了标配,在此大背景下,ASP.NET Core也应运而生。本文主要利用ASP.NET Core开发一个学生管理系统为例,简述ASP.NET Core开发的常见知识点,仅供学习分享使用,如有不足之处,还请指正。 涉及知识点 开发学生管理系统,涉及知识点,如下所示: 开发工具:Visual Studio 2019 目标框架:.Net 5.0 架构:MVC三层架构【Model-View-Controller】 文件-->新建-->项目-->
数据源与控件的BindingWPF绑定模式(mode)是枚举的 枚举值共有5个 1:OneWay(源变就更新目标属性) 2:TwoWay(源变就更新目标并且目标变就更新源) 3:OneTime(只根据源来设置目标,以后都不会变) 4:OneWayToSource(与OneWay相反) 5:Default(可以单向或双向,是靠被值定的源或目标是否有get或set来指定的) 所以绑定的话...
```xml <Window x:Class="ReoGridWpfDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:rg="clr-namespace:unvell.ReoGrid;assembly=unvell.ReoGrid.WPF"> <rg:ReoGridControl x:Name="grid" /> </Grid> </Window> 后台代码: ```csharp using unvell.ReoGrid; using unvell.ReoGrid.CellTypes; using unvell.ReoGrid.DataFormat; using unvell.ReoGrid.Graphics; public partial class MainWindow : Window public MainWindow() InitializeComponent(); // 在代码中配置ReoGrid控件 grid.LoadRGFile("test.rgf"); RangeStyle style = new RangeStyle(); style.Flag = PlainStyleFlag.FontSize | PlainStyleFlag.Bold; style.FontSize = 16; style.Bold = true; grid.SetRangeStyle("A1:G1", style); grid.SetCellData("A2", "Hello, ReoGrid!"); TextFormat format = new TextFormat(); format.Underline = TextUnderlineType.Single; format.FontColor = SolidColor.Red; grid.SetRangeDataFormat("A2:G2", format); grid.SetColsWidth(1, 7, 100); grid.SetRowsHeight(1, 2, 50); grid.SetRangeBorders("A1:G2", BorderPositions.All, new RangeBorderStyle Color = SolidColor.Black, Style = BorderLineStyle.Solid 这个示例将一个ReoGrid控件显示在WPF窗口中,并在代码中对其进行了一些配置和操作。