【问题标题】:Databinding to an Object property fails, to the parent it is working到对象属性的数据绑定失败,它正在工作的父级
【发布时间】:2017-09-22 00:20:36
【问题描述】:

我尝试将属性绑定到嵌套对象,但失败了。

我已经查看了这些问题,但我认为我在其他地方犯了另一个错误。也许有人可以让我后退。

WPF: How to bind to a nested property?

binding to a property of an object

到上面的滑块/文本框有一个正确的绑定,而下面的没有这样做。

我有两个带有对应文本框的滑块:

<StackPanel>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBox Text="{Binding Path= boundnumber, Mode=TwoWay, FallbackValue='binding failed'}" ></TextBox>
    <Slider Value="{Binding Path= boundnumber, Mode=TwoWay}" Width="500" Maximum="1000" ></Slider>
</StackPanel>

    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" DataContext="{Binding Path=myDatarow}">

        <TextBox Text="{Binding Path= boundnumber, Mode=TwoWay, FallbackValue='binding failed'}" ></TextBox>
        <Slider Value="{Binding Path= boundnumber, Mode=TwoWay}" Width="500" Maximum="1000" ></Slider>
</StackPanel>
</StackPanel>

后面的代码:

public partial class MainWindow : INotifyPropertyChanged
    {
        public MainWindow()
        {
            DataContext = this;
            InitializeComponent();
        }

        private int _boundnumber;
        public int boundnumber
        {
            get { return _boundnumber; }
            set
            {
                if (value != _boundnumber)
                {
                    _boundnumber = value;
                    OnPropertyChanged();
                }
            }
        }

        Datarow myDatarow = new Datarow(11);

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string propertyname = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
        }
    }

class Datarow : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged([CallerMemberName] string propertyname = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
    }

    public Datarow()
    {

    }

    public Datarow(int number)
    {
        boundnumber = number;
    }

    private int _boundnumber;
    public int boundnumber
    {
        get { return _boundnumber; }
        set
        {
            if (value != _boundnumber)
            {
                _boundnumber = value;
                OnPropertyChanged();
            }
        }
    }
}

【问题讨论】:

    标签: c# wpf data-binding binding nested


    【解决方案1】:

    您需要将您的 myDatarow 公开到像您的 boundnumber 这样的公共财产中。

        private DataRow _myDatarow = new DataRow(11);
        public DataRow myDataRow
        {
            get { return _myDatarow; }
        }
    

    还有一个额外的建议。 最好将 DataContext 类与 MainWindow 分开。

    【讨论】:

    • 解决问题的一个关键是将类本身声明为公共
    猜你喜欢
    • 2013-05-03
    • 2013-01-20
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 2012-12-14
    • 2018-07-27
    相关资源
    最近更新 更多