【问题标题】:Add items to gridview / listview programmatically以编程方式将项目添加到 gridview / listview
【发布时间】:2014-10-26 12:50:47
【问题描述】:

我的 GridView 有这个模板:

  <DataTemplate x:Key="GroupTemplate1">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

            <Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" Margin="0,9.5,0,0">
                <Image Source="{Binding Property3}" Height="79" Width="79"/>
            </Border>

            <!-- <StackPanel Grid.Column="1" Margin="14.5,0,0,0">
                <TextBlock Text="{Binding Property1}" Style="{ThemeResource ListViewItemTextBlockStyle}"/>
                <TextBlock Text="{Binding Property2}" Style="{ThemeResource ListViewItemSubheaderTextBlockStyle}"/>
            </StackPanel> -->

        </Grid>
    </DataTemplate>

如您所见,每个项目都有三个信息:Property3(图像)、Property2(文本)、Property1(文本)。

如何使用这三个字段创建和添加一个新的 ListViewItem 并以编程方式将其添加到 GridView?

类似:

//
gridView1.Add(new ListViewItem("imagename", "text1", "text2");
//

【问题讨论】:

  • 是否有特殊原因您没有将 GridView 包装在 ListView 中并将 ListView 的 ItemsSource 设置为与 GroupTemplate 匹配的项目的集合?这是 MVVM 非常适合解决的问题,不会让人头疼,也不需要以编程方式向 UI 添加内容。
  • 因为我必须在应用执行期间更改网格视图的图像

标签: c# listview gridview windows-phone-8


【解决方案1】:

您似乎正在尝试同时进行 MVVM 和非 MVVM。使用预制的 DataTemplate 和绑定,您无法真正按照上面的方式做事。通常,您可以选择在 XAML 中预先构建 ListView,在这种情况下设计或多或少是静态的,或者将 ListView 绑定到集合并使用 INotifyPropertyChanged 让 UI 识别它需要更新视图。了解 WPF 的大部分功能来自于很少需要手动将项目添加到 UI 控件或根本不需要更改它们。

这是一个非常简单的实现:

这将是每个 GridViewItem 从中获取信息的对象 - Property1、Property2 等的来源:

class ModelSample
{
    public object Property1 { get; set; } // anything you ever bind to *must* be a public property
    public object Property2 { get; set; }
    public object Property3 { get; set; }
}

这是一个示例 ViewModel 的实现:

class ViewModelSample : INotifyPropertyChanged  // uses System.ComponentModel
{
    private List<ModelSample> models = new List<ModelSample>();
    public List<ModelSample> Models 
    { 
        get { return models; }
        set
        {
            models = value;
            NotifyPropertyChanged("Models");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }   
}

然后您将实例化 ViewModelSample 并设置 DataContext:

var viewModelSample = new ViewModelSample();
// add your objects to viewModelSample.Models
this.DataContext = viewModelSample.

并将 ListView 上的 ItemsSource 属性绑定到模型。任何时候你想改变 ListView 的图像,只需修改 Models 的内容,UI 应该立即反映集合的当前状态。

这可能看起来比您习惯的要复杂一些,但是一旦您了解了使用 NotifyPropertyChanged 来提醒 UI 的基本想法,您会发现它实际上少了很多工作,而 WPF 几乎可以处理一切都给你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-24
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 2018-06-19
    • 2013-09-09
    相关资源
    最近更新 更多