繁体   English   中英

从代码访问数据模板(列表视图)的控件

[英]Access a control of a datatemplate (listview) from code

我有一个带有自定义数据模板的listview,每个ListViewItem都有一个文本,一个作者和一个日期。 像这样

texttexttexttext
Author     Date

现在,我想创建多个项目并针对这三个文本框的每个项目进行调整。

通常,您可以使用FindName方法( MSDN )执行此操作,但是Windows 8 WinRT框架似乎缺少此方法,而且我还没有找到另一种方法来完成此操作。

您应该使用数据绑定:

<ListView ItemsSource="{Binding List}">
     <ListView.ItemTemplate>
          <DataTemplate>
              <TextBlock Text="{Binding Author}" />
          </DataTemplate>
     </ListView.ItemTemplate>
</List>

列表在视图模型和ObservableCollection<Item>

项目:

public class Item : INofifyPropertyChanged
{
    private string author; 
    public string Author
    {
        get { return author; }
        set 
        {
            author = value;
            var copy = PropertyChanged; // avoid concurrent changes
            if (copy != null)
                copy(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    ...
}

在互联网上搜索更完整的绑定教程...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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