C# 具有网格高度的数据绑定类属性

C# 具有网格高度的数据绑定类属性,c#,wpf,data-binding,C#,Wpf,Data Binding,我有以下课程: public class Height { public GridLength GridHeight = new GridLength(200); } 我想将此字段绑定到网格的第一行高度: <Grid.RowDefinitions> <RowDefinition Height="{Binding Path=GridHeight, Mode=OneWay}"></RowDefinition> &l

我有以下课程:

public class Height
{
    public GridLength GridHeight = new GridLength(200);
}
我想将此字段绑定到网格的第一行高度:

    <Grid.RowDefinitions>
        <RowDefinition Height="{Binding Path=GridHeight, Mode=OneWay}"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
然而,根本没有互动。我看不出有什么不对。
非常感谢有人能解释如何将类属性数据绑定到行高属性。

您需要将
GridHeight
设置为属性,而不是字段

如果希望更新
Height
类,可能还需要为该类实现

public class Height
{
    public Height() 
    {
        this.GridHeight = new GridLength(200);
    }

    // Needs to be a property for data binding
    public GridLength GridHeight { get; set; }
}

绑定仅适用于属性而不适用于字段,并且仅在实现Inotifypropertychanged接口时更新

因此,无论何时更新GridHeight上的值,网格都会自动更新其值

public class Height:INotifyPropertyChanged
{
    GridLength _gridheight = new GridLength(200);
    public GridLength GridHeight
            {
                    get{
                            return _gridheight;
                       }

                    set{
                            if(_gridheight==value)
                               return;

                            _gridheight=value;
                            NotifyPropertyChanged("GridHeight")
                        }

         }

  public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

非常感谢您提及InotifyProperty更改!
public class Height:INotifyPropertyChanged
{
    GridLength _gridheight = new GridLength(200);
    public GridLength GridHeight
            {
                    get{
                            return _gridheight;
                       }

                    set{
                            if(_gridheight==value)
                               return;

                            _gridheight=value;
                            NotifyPropertyChanged("GridHeight")
                        }

         }

  public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}