WPF C#记录依赖项对象数据修改
我在这里处理依赖项对象,以前对它们做得不多,但它们看起来非常有用 基本上,我使用这些对象在ListView中进行编辑。但是,我必须将这些更改写回SQL。我的问题是,是否有一种方法可以记录数据是否已被修改,因为我不想每次有人查看数据时都回写SQL。目前我有:WPF C#记录依赖项对象数据修改,c#,wpf,dependency-properties,C#,Wpf,Dependency Properties,我在这里处理依赖项对象,以前对它们做得不多,但它们看起来非常有用 基本上,我使用这些对象在ListView中进行编辑。但是,我必须将这些更改写回SQL。我的问题是,是否有一种方法可以记录数据是否已被修改,因为我不想每次有人查看数据时都回写SQL。目前我有: public class KPI : DependencyObject { public static readonly DependencyProperty DepartmentProperty = DependencyProper
public class KPI : DependencyObject
{
public static readonly DependencyProperty DepartmentProperty = DependencyProperty.Register("Department", typeof(string), typeof(KPI), new UIPropertyMetadata(null));
public string Department
{
get { return (string)GetValue(DepartmentProperty); }
set { SetValue(DepartmentProperty, value); }
}
public static readonly DependencyProperty KPINumberProperty = DependencyProperty.Register("KPINumberProperty", typeof(int), typeof(KPI), new UIPropertyMetadata(null));
public int KPINumber
{
get { return (int)GetValue(KPINumberProperty); }
set { SetValue(KPINumberProperty, value); }
}
}
我的想法是要有这样的东西:
public static bool DataModified = false;
public static readonly DependencyProperty DepartmentProperty = DependencyProperty.Register("Department", typeof(string), typeof(KPI), new UIPropertyMetadata(null));
public string Department
{
get { return (string)GetValue(DepartmentProperty); }
set { SetValue(DepartmentProperty, value); DataModified = true; }
}
因此,每次编辑某个内容时,DataModified属性都将设置为TRUE,这是一种好方法吗?还是有人有更好的方法
提前谢谢
Sumgay。如果绑定到依赖项属性,这实际上不起作用。WPF绑定引擎实际上并不使用CLR“Department”属性,而是直接在依赖项属性上使用“SetValue”。不过,有一个简单的解决办法 UIPropertyMetadata有一个PropertyChangedCallback字段,每次属性值更改时(从直接调用SetValue,或通过包装SetValue调用的CLR属性)都会触发该字段 下面是一个例子:
public static readonly DependencyProperty DepartmentProperty =
DependencyProperty.Register("Department",
typeof(string),
typeof(KPI),
new UIPropertyMetadata(null, DepartmentPropertyChanged));
private static void DepartmentPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
KPI me = d as KPI;
if (me == null) return;
// Talk to your Business/Data layers here
}
public string Department
{
get { return (string)GetValue(DepartmentProperty); }
set { SetValue(DepartmentProperty, value); }
}
DependencyObject(d)是属性所属的对象。在您的情况下,这将是KPI的一个实例
作为参考,这里有一个指向UIPropertyMetadata MSDN文档的链接:如果您绑定到dependency属性,这实际上将不起作用。WPF绑定引擎实际上并不使用CLR“Department”属性,而是直接在依赖项属性上使用“SetValue”。不过,有一个简单的解决办法 UIPropertyMetadata有一个PropertyChangedCallback字段,每次属性值更改时(从直接调用SetValue,或通过包装SetValue调用的CLR属性)都会触发该字段 下面是一个例子:
public static readonly DependencyProperty DepartmentProperty =
DependencyProperty.Register("Department",
typeof(string),
typeof(KPI),
new UIPropertyMetadata(null, DepartmentPropertyChanged));
private static void DepartmentPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
KPI me = d as KPI;
if (me == null) return;
// Talk to your Business/Data layers here
}
public string Department
{
get { return (string)GetValue(DepartmentProperty); }
set { SetValue(DepartmentProperty, value); }
}
DependencyObject(d)是属性所属的对象。在您的情况下,这将是KPI的一个实例
作为参考,这里有一个指向UIPropertyMetadata MSDN文档的链接:WPF绑定系统不一定调用
部门
CLR属性,而是在更新依赖项属性时直接调用设置值
(在本例中为部门
)。这意味着,您的CLR包装可能不会被调用,这反过来意味着您在set
block(属于Department
CLR属性)中编写的任何代码都不会被执行
但别担心,有解决办法。初始化DepartmentProperty
时,可以将回调传递给UIPropertyMetadata
,每次更新依赖项属性时都会调用该回调。这意味着,您必须实现以下目标:
public static readonly DependencyProperty DepartmentProperty = DependencyProperty.Register
(
"Department",
typeof(string),
typeof(KPI),
new UIPropertyMetadata(null, OnDepartmentChanged)
);
public string Department
{
get { return (string)GetValue(DepartmentProperty); }
set { SetValue(DepartmentProperty, value);}
}
static void OnDepartmentChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
(d as KPI).DataModified = true; //this is all you want!
}
WPF绑定系统不一定调用
Department
CLR属性,而是在更新依赖项属性时直接调用SetValue
(在本例中为Department
)。这意味着,您的CLR包装可能不会被调用,这反过来意味着您在set
block(属于Department
CLR属性)中编写的任何代码都不会被执行
但别担心,有解决办法。初始化DepartmentProperty
时,可以将回调传递给UIPropertyMetadata
,每次更新依赖项属性时都会调用该回调。这意味着,您必须实现以下目标:
public static readonly DependencyProperty DepartmentProperty = DependencyProperty.Register
(
"Department",
typeof(string),
typeof(KPI),
new UIPropertyMetadata(null, OnDepartmentChanged)
);
public string Department
{
get { return (string)GetValue(DepartmentProperty); }
set { SetValue(DepartmentProperty, value);}
}
static void OnDepartmentChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
(d as KPI).DataModified = true; //this is all you want!
}
在什么情况下,绑定引擎会调用Department CLR属性而不是直接调用SetValue?@Robin:很难说;在某些情况下,它可能调用CLR包装器,在某些情况下,它可能直接调用
GetValue
和SetValue
方法。此外,CLR属性在XAML中显示名称具有智能意义。我同意制作CLR属性是更好的做法,但我从未见过或读过绑定引擎使用它们而不是GetValue或SetValue的任何时候。不过我相信你的话。在什么情况下,绑定引擎会调用Department CLR属性而不是直接调用SetValue?@Robin:很难说;在某些情况下,它可能调用CLR包装器,在某些情况下,它可能直接调用GetValue
和SetValue
方法。此外,CLR属性在XAML中显示名称具有智能意义。我同意制作CLR属性是更好的做法,但我从未见过或读过绑定引擎使用它们而不是GetValue或SetValue的任何时候。不过我相信你的话。