简体   繁体   中英

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)

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.

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

You're firing the property changed event on a property called 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. 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.

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.

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. 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 ).

  1. Create a new instance of EditorSelectionTool , assign this to the property and notify the change

or

  1. Implement a change notification on each property in EditorSelectionTool

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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