【问题标题】:Access a control of a datatemplate (listview) from code从代码访问数据模板(列表视图)的控件
【发布时间】:2012-08-12 18:06:32
【问题描述】:

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

texttexttexttext
Author     Date

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

【问题讨论】:

  • 这个问题还不清楚。您想要为每个项目使用不同的数据模板(布局),还是想要为您添加的每个项目填充不同的文本/数据的 3 个文本框?
  • 我猜想有比使用代码隐藏更好的方法来定位它们。给我们更多细节,我们可以弄清楚。
  • 我希望每次文本都不同。希望现在清楚了。

标签: c# xaml listview windows-8 microsoft-metro


【解决方案1】:

你应该使用数据绑定:

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

列表在视图模型和ObservableCollection&lt;Item&gt; 中定义。

项目:

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));
        }
    }

    ...
}

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

【讨论】:

    【解决方案2】:

    通常您可以使用 FindName 方法 (MSDN) 执行此操作,但 Windows 8 WinRT 框架中似乎缺少此方法,我还没有找到其他方法来完成此操作...

    【讨论】:

    • mhm...还有其他方法可以更改每个 ListViewItem 上的文本吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多