简体   繁体   中英

Access a control of a datatemplate (listview) from code

I have a listview with a custom datatemplate, that each ListViewItem has a text, an author and a date. Something like this

texttexttexttext
Author     Date

Now I want to create multiple Items and adjust on every item these three textboxes.

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

You should use data bindings:

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

List is defined in view model and an ObservableCollection<Item> .

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

    ...
}

Search the internet for more complete binding tutorials...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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