【问题标题】:Why is my Binding only working one way?为什么我的绑定只能以一种方式工作?
【发布时间】:2016-10-07 13:44:46
【问题描述】:

我正在使用以下 xaml 代码:

  <ListView ItemsSource="{Binding Offsets, Mode=TwoWay}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="X" Width="90">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding X, Mode=TwoWay}" 
                                     Width="70"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

(我知道这看起来有点草率,但我尽量保持最小)

我绑定的 Offsets 属性是 public List&lt;Point3D&gt; { get; set;}。 (using System.Windows.Media.Media3D)。

因此,每个 Point3D 都有一个公共 X、Y 和 Z 属性。

我的 ListView 生成正常,但是当我尝试更改 TextBox 的值时,Datacontext 没有更新。

我做错了什么?

【问题讨论】:

  • 你实现了 INotifyPropertyChanged 接口?
  • riteshmeher 是对的。你也可以使用 ObservableCollection&lt;Point3D&gt; 来实现这个。
  • 所以你正在编辑TextBox并且set没有被调用?

标签: c# wpf data-binding binding


【解决方案1】:

如果你说的是Point3D Structure
是的,它有 XYZ 公共属性,但我认为它没有实现 INotifyPropertyChanged

【讨论】:

  • 所以只需在其上放置一个实现 INotifyPropertyChanged 的​​包装类(或结构)
  • 坦克,这就是我刚刚做的:)
【解决方案2】:

您可能忘记在模型或视图模型类中实现接口INotifyPropertyChanged

【讨论】:

  • 谢谢,我忘记了。
【解决方案3】:

你需要在你的类中实现 INotifyPropertyChanged 接口。你的绑定模式很好。您应该能够看到更改。

public class YourClassName: INotifyPropertyChanged
{
 public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
public ObservableCollection<YourModel> Offsets
    {
        get {return this.offsets;}

        set
        {
            if (value != this.offsets)
            {
                this.offsets= value;
                NotifyPropertyChanged("Offsets");
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 2015-08-04
    • 2014-07-16
    • 2016-01-31
    • 1970-01-01
    • 2021-04-16
    相关资源
    最近更新 更多