【问题标题】:C# WinRT Data Binding to "this" or using the index of the item within a collectionC# WinRT 数据绑定到“this”或使用集合中项目的索引
【发布时间】:2014-10-14 04:47:38
【问题描述】:

我正在使用 WinRT 和 .Net 4.5。我有一个带有 ItemTemplates 的 ListView,它们的数据成员绑定到每行项目中的不同字段。让我们将我的收藏称为Series,然后将系列中的项目称为Item

我的行中有文本字段,我在这些字段上接收 TextChanged 事件。我希望能够更新我收藏中的Item。当我为 iOS 或 Mac 实现此功能时,我会将文本字段上的 tag 设置为 Item 的索引。这样,当我收到 TextChanged 事件时,我可以按索引查找 Item,然后更新我的数据模型。

对于 WinRT,TextBox 上的 Tag 对象是 object 类型,所以我想我可以将 Tag 字段直接绑定到我的 Item 对象。然后我就不必从Series 中查找它,它可以直接从我的事件方法中的sender 获得。

我尝试使用Tag="{Binding this}",但这导致Tag 在事件处理程序期间仍然为空。

有没有办法将每一行中的Tags 绑定到Series 中的Item 对象?

<TextBox x:Name="Initial_InputAmount" Tag="{Binding this}" 
    Text="{Binding amountString}" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" 
    Margin="0, 0, 10, 0" Width="120" TextChanged="textChangedAmount"/>

或者,有没有办法可以绑定到 ListView 中项目的索引,例如:

Tag="{Binding indexOfThis}"

因为这将使我能够在我的事件处理程序期间轻松地从Series 查找我的Item

【问题讨论】:

    标签: c# xaml listview data-binding windows-runtime


    【解决方案1】:

    你可以离开

    Tag="{Binding}"
    

    它将绑定到项目的当前 DataContext - 您的视图模型。

    但我不确定你是否真的需要它——而不是使用 textChangedAmount 事件来更改你的模型(视图模型),你可以处理属性设置器上的所有更改:

    public class ItemModel
    {
        public String amountString 
        { 
            get 
            {
                return this._amountString;
            };
            set
            { 
                if (this._amountString == value)
                    return;
    
                HandleChanges(value);
    
                this._amountString = value;
                this.OnPropertyChanged("amountString");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      • 2019-07-17
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多