到 属性 的数据绑定正在运行,但未更新
Databinding to property is working, but not updating
我有 属性 型号:
private EditorSelectionTool SelectionTool { get; set; }
绑定:
<DataTemplate DataType="{x:Type viewModels:EditorSelectionTool}">
<Rectangle Stroke="White" StrokeThickness="1" StrokeDashArray="4 4" Width="{Binding Width}" Height="{Binding Height}" Visibility="{Binding Visibility}"/>
</DataTemplate>
模型派生自 PropertyChangedBase class (Caliburn.Micro)
以及更改 属性 字段的方法:
public void StartSelecting(Point point)
{
SelectionTool.X = point.X;
SelectionTool.Y = point.Y;
NotifyOfPropertyChange(() => SelectionTool);
}
调试显示调用了方法。但是 UI 的变化并没有发生。
选择工具class:
public class EditorSelectionTool
{
public double X { get; set; }
public double Y { get; set; }
public double Width { get; set; }
public double Height { get; set; }
public Visibility Visibility { get; set; }
}
您正在 属性 名为 SelectionTool 的 属性 changed 事件中触发。但是,您从未在高度和可见性上触发 属性 更改事件。
在某些情况下,这会起作用——这取决于某些东西是否实际绑定到 SelectionTool。在这种情况下,它会强制绑定进一步 "down the chain" 也重新加载。但由于您遇到了问题,我认为这不适用于您的特定绑定场景。
在Visual Studio中,输出window经常有绑定失败的信息。
看到您正在使用 Caliburn.Micro,您将需要更新 EditorSelectionTool
以实施 PropertyChangedBase
,正如 Paul K 提到的那样,向每个 属性 添加更改通知=16=].
public class EditorSelectionTool : PropertyChangedBase
{
private double _x;
private double _y;
private double _width;
private double _height;
private Visibility _visibility;
public double X
{
get { return _x; }
set
{
_x = value;
NotifyOfPropertyChange(()=> X);
}
}
public double Y
{
get { return _y; }
set
{
_y = value;
NotifyOfPropertyChange(() => Y);
}
}
public double Width
{
get { return _width; }
set
{
_width = value;
NotifyOfPropertyChange(() => Width);
}
}
public double Height
{
get { return _height; }
set
{
_height = value;
NotifyOfPropertyChange(() => Height);
}
}
public Visibility Visibility
{
get { return _visibility; }
set
{
_visibility = value;
NotifyOfPropertyChange(() => Visibility);
}
}
}
您在 SettingTool
通知更改,但 属性 根本没有更改。这是同一个实例,因此没有变化。
即使您通知更改,WPF 也会通过比较旧值和当前值(这里是 SettingTool
的实例)来检查是否真的有更改。
- 创建
EditorSelectionTool
的新实例,将其分配给 属性 并通知更改
或
- 在
EditorSelectionTool
属性上实施变更通知
我有 属性 型号:
private EditorSelectionTool SelectionTool { get; set; }
绑定:
<DataTemplate DataType="{x:Type viewModels:EditorSelectionTool}">
<Rectangle Stroke="White" StrokeThickness="1" StrokeDashArray="4 4" Width="{Binding Width}" Height="{Binding Height}" Visibility="{Binding Visibility}"/>
</DataTemplate>
模型派生自 PropertyChangedBase class (Caliburn.Micro)
以及更改 属性 字段的方法:
public void StartSelecting(Point point)
{
SelectionTool.X = point.X;
SelectionTool.Y = point.Y;
NotifyOfPropertyChange(() => SelectionTool);
}
调试显示调用了方法。但是 UI 的变化并没有发生。
选择工具class:
public class EditorSelectionTool
{
public double X { get; set; }
public double Y { get; set; }
public double Width { get; set; }
public double Height { get; set; }
public Visibility Visibility { get; set; }
}
您正在 属性 名为 SelectionTool 的 属性 changed 事件中触发。但是,您从未在高度和可见性上触发 属性 更改事件。
在某些情况下,这会起作用——这取决于某些东西是否实际绑定到 SelectionTool。在这种情况下,它会强制绑定进一步 "down the chain" 也重新加载。但由于您遇到了问题,我认为这不适用于您的特定绑定场景。
在Visual Studio中,输出window经常有绑定失败的信息。
看到您正在使用 Caliburn.Micro,您将需要更新 EditorSelectionTool
以实施 PropertyChangedBase
,正如 Paul K 提到的那样,向每个 属性 添加更改通知=16=].
public class EditorSelectionTool : PropertyChangedBase
{
private double _x;
private double _y;
private double _width;
private double _height;
private Visibility _visibility;
public double X
{
get { return _x; }
set
{
_x = value;
NotifyOfPropertyChange(()=> X);
}
}
public double Y
{
get { return _y; }
set
{
_y = value;
NotifyOfPropertyChange(() => Y);
}
}
public double Width
{
get { return _width; }
set
{
_width = value;
NotifyOfPropertyChange(() => Width);
}
}
public double Height
{
get { return _height; }
set
{
_height = value;
NotifyOfPropertyChange(() => Height);
}
}
public Visibility Visibility
{
get { return _visibility; }
set
{
_visibility = value;
NotifyOfPropertyChange(() => Visibility);
}
}
}
您在 SettingTool
通知更改,但 属性 根本没有更改。这是同一个实例,因此没有变化。
即使您通知更改,WPF 也会通过比较旧值和当前值(这里是 SettingTool
的实例)来检查是否真的有更改。
- 创建
EditorSelectionTool
的新实例,将其分配给 属性 并通知更改
或
- 在
EditorSelectionTool
属性上实施变更通知