代码之家  ›  专栏  ›  技术社区  ›  Matt Mitchell

如何以编程方式将绑定转换器添加到WPF列表视图?

  •  6
  • Matt Mitchell  · 技术社区  · 14 年前

    创建、填充和设置列表视图的样式。我发现的每个示例都倾向于使用大量XAML标记和最少的C#来切换正在运行的标记位。这对我来说是不可能的,因为在编译时我不知道列的组成,也不知道预期的样式。

    我(简化)的是这样的:

    string[] columns = new string[] { "Test", "Test2" };
    ListView lv = new ListView();
    
    /* Add Columns (works */
    GridView viewLayout = new GridView();
    foreach (string colName in columns)
    {
        viewLayout.Columns.Add(new GridViewColumn{ Header = colName });
    }
    lv.View = viewLayout;
    
    /* Add Items (happy to go the .source path if it's easier) */
    foreach (object d in GetData())
    {
         lv.Items.Add(d);
    }
    
    /* Example style, fails */
    lv.ItemContainerStyle.Setters.Add(
     new Setter(Control.BackgroundProperty, *how do you connect the IValueConverter*)
    );
    
    1 回复  |  直到 14 年前
        1
  •  39
  •   Arcturus    14 年前

    使用绑定,设置转换器属性;)

    new Binding() { Converter = new MyAwesomeConverter() }