简体   繁体   English

数据绑定到属性有效,但无法更新

[英]Databinding to property is working, but not updating

I have property in model: 我在模型中有属性:

private EditorSelectionTool SelectionTool { get; set; }

Binding: 捆绑:

 <DataTemplate DataType="{x:Type viewModels:EditorSelectionTool}">
                    <Rectangle Stroke="White" StrokeThickness="1" StrokeDashArray="4 4" Width="{Binding Width}" Height="{Binding Height}" Visibility="{Binding Visibility}"/>
                </DataTemplate>

Model is derivated from PropertyChangedBase class (Caliburn.Micro) 模型是从PropertyChangedBase类(Caliburn.Micro)派生的

And method which change the property fields: 以及更改属性字段的方法:

 public void StartSelecting(Point point)
    {
        SelectionTool.X = point.X;
        SelectionTool.Y = point.Y;
        NotifyOfPropertyChange(() => SelectionTool);
    }

Debug shows that method was called. 调试显示该方法已被调用。 But change in UI is not happening. 但是,UI的更改并未发生。

SelectionTool class: SelectionTool类:

   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; }
}

You're firing the property changed event on a property called SelectionTool. 您将在名为SelectionTool的属性上触发属性更改事件。 However you've never fired the property changed event on Height and Visibility. 但是,您从未在“高度和可见性”上触发属性更改事件。

In some cases this would work--it depends on if something is actually bound to SelectionTool. 在某些情况下,这会起作用-这取决于是否确实将某些东西绑定到SelectionTool。 In such a case it would force the bindings further "down the chain" to also reload. 在这种情况下,它将迫使绑定进一步“下降”以重新加载。 But since you're having problems, I assume that doesn't apply to your specific binding scenario. 但是由于您遇到问题,我认为这不适用于您的特定绑定方案。

In Visual Studio, the output window often has information about binding failures. 在Visual Studio中,输出窗口通常包含有关绑定失败的信息。

Seeing you are using Caliburn.Micro, you will need to update your EditorSelectionTool to implement PropertyChangedBase and as Paul K mentioned, add property change notifications to each property. 看到您正在使用Caliburn.Micro,您将需要更新EditorSelectionTool来实现PropertyChangedBase并如Paul K所述,向每个属性添加属性更改通知。

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);
        }
    }
}

You notify a change on SettingTool , but that property has not changed at all. 您在SettingTool上通知了更改,但是该属性完全没有更改。 It is the same instance and therefore there is no change. 这是同一实例,因此没有任何变化。

And even if you notify a change, WPF will check, if there is really a change by comparing the old with the current value (here the instance of SettingTool ). 即使您通知了更改,WPF也会通过将旧值与当前值(此处为SettingTool的实例)进行比较来检查是否确实存在更改。

  1. Create a new instance of EditorSelectionTool , assign this to the property and notify the change 创建的新实例EditorSelectionTool ,这个分配的财产和更改通知

or 要么

  1. Implement a change notification on each property in EditorSelectionTool EditorSelectionTool 每个属性上实施更改通知

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM