C# 为什么PropertyChangedEvent对一个AttachedProperty只开火一次
OnShouldRefreshPropertyChanged仅触发一次,并且在更改NeedsRefresh的值时不会触发。有人能解释一下发生了什么事吗?我使用的Datagrid来自.NET3.5的WPFToolKit,尽管我认为这并不重要 行为:C# 为什么PropertyChangedEvent对一个AttachedProperty只开火一次,c#,.net,wpfdatagrid,attached-properties,attachedbehaviors,C#,.net,Wpfdatagrid,Attached Properties,Attachedbehaviors,OnShouldRefreshPropertyChanged仅触发一次,并且在更改NeedsRefresh的值时不会触发。有人能解释一下发生了什么事吗?我使用的Datagrid来自.NET3.5的WPFToolKit,尽管我认为这并不重要 行为: public static class DataGridRefreshBehavior { // Using a DependencyProperty as the backing store for ShouldRefresh. This
public static class DataGridRefreshBehavior
{
// Using a DependencyProperty as the backing store for ShouldRefresh. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ShouldRefreshProperty =
DependencyProperty.RegisterAttached("ShouldRefresh",
typeof(bool),
typeof(DataGridRefreshBehavior),
new UIPropertyMetadata(false, OnShouldRefreshPropertyChanged));
public static void SetShouldRefresh(DependencyObject obj, bool value)
{
if (obj != null)
obj.SetValue(ShouldRefreshProperty, value);
}
public static bool GetShouldRefresh(DependencyObject obj)
{
if (obj != null)
return (bool)obj.GetValue(ShouldRefreshProperty);
else
return false;
}
private static void OnShouldRefreshPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var attachedObject = sender as DataGrid;
if (attachedObject != null)
{
bool needsRefresh = (bool)e.NewValue;
if (needsRefresh)
{
attachedObject.Items.Refresh();
}
}
}
}
local:DataGridRefreshBehavior.ShouldRefresh="{Binding Path=NeedsRefresh,UpdateSourceTrigger=PropertyChanged}"
public bool NeedsRefresh
{
get
{
return needsRefresh;
}
set
{
needsRefresh = value;
NotifyOfPropertyChange(() => NeedsRefresh);
}
}
Xaml:
public static class DataGridRefreshBehavior
{
// Using a DependencyProperty as the backing store for ShouldRefresh. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ShouldRefreshProperty =
DependencyProperty.RegisterAttached("ShouldRefresh",
typeof(bool),
typeof(DataGridRefreshBehavior),
new UIPropertyMetadata(false, OnShouldRefreshPropertyChanged));
public static void SetShouldRefresh(DependencyObject obj, bool value)
{
if (obj != null)
obj.SetValue(ShouldRefreshProperty, value);
}
public static bool GetShouldRefresh(DependencyObject obj)
{
if (obj != null)
return (bool)obj.GetValue(ShouldRefreshProperty);
else
return false;
}
private static void OnShouldRefreshPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var attachedObject = sender as DataGrid;
if (attachedObject != null)
{
bool needsRefresh = (bool)e.NewValue;
if (needsRefresh)
{
attachedObject.Items.Refresh();
}
}
}
}
local:DataGridRefreshBehavior.ShouldRefresh="{Binding Path=NeedsRefresh,UpdateSourceTrigger=PropertyChanged}"
public bool NeedsRefresh
{
get
{
return needsRefresh;
}
set
{
needsRefresh = value;
NotifyOfPropertyChange(() => NeedsRefresh);
}
}
视图模型:
public static class DataGridRefreshBehavior
{
// Using a DependencyProperty as the backing store for ShouldRefresh. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ShouldRefreshProperty =
DependencyProperty.RegisterAttached("ShouldRefresh",
typeof(bool),
typeof(DataGridRefreshBehavior),
new UIPropertyMetadata(false, OnShouldRefreshPropertyChanged));
public static void SetShouldRefresh(DependencyObject obj, bool value)
{
if (obj != null)
obj.SetValue(ShouldRefreshProperty, value);
}
public static bool GetShouldRefresh(DependencyObject obj)
{
if (obj != null)
return (bool)obj.GetValue(ShouldRefreshProperty);
else
return false;
}
private static void OnShouldRefreshPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var attachedObject = sender as DataGrid;
if (attachedObject != null)
{
bool needsRefresh = (bool)e.NewValue;
if (needsRefresh)
{
attachedObject.Items.Refresh();
}
}
}
}
local:DataGridRefreshBehavior.ShouldRefresh="{Binding Path=NeedsRefresh,UpdateSourceTrigger=PropertyChanged}"
public bool NeedsRefresh
{
get
{
return needsRefresh;
}
set
{
needsRefresh = value;
NotifyOfPropertyChange(() => NeedsRefresh);
}
}
它是根本没有发射还是
e.NewValue
返回为false
?。NotiyfOfPropertyChange(…)
在做什么?您通常必须在代码中指定DependencyPropertyChangedEventArgs
的值,您是否曾经手动设置/更新ShouldRefresh
属性(通过调用SetShouldRefresh()
)?这将破坏绑定(默认为单向),并忽略对NeedsRefresh
的进一步更改。尝试将绑定模式设置为双向,看看是否有帮助:。Path=NeedsRefresh,mode=TwoWay..
。它第一次激发,然后甚至不激发,我不是手动调用SetShouldRefresh,我所做的就是调用NeedsRefresh=true。NotipfyPropertyChange类似于OnPropertyChanged(“NeedsRefresh”),您只设置了NeedRefresh=true
?您需要在设置为true和false之间进行切换,因此值实际上会发生更改。我的印象是,如果调用NotifyPropertyChanged,则无论值是否实际更改,都将读取该值。谢谢你帮我认识到我的愚蠢,我现在就躲到地下室去:)