【问题标题】:Bind a custom class of controls to Listview/GridView as ListViewItem将自定义控件类绑定到 Listview/GridView 作为 ListViewItem
【发布时间】:2014-03-13 23:19:32
【问题描述】:

我在将数据绑定到 ListView/GridView 中的控件作为 ListViweItem 时遇到一些问题。

要求:每行必须显示一个复选框、标签、下拉菜单和一个文本框,从所有不同来源获取数据。这是示例类:

class Data {
    public CheckBox chk_box;
    public Label lbl;
    public ComboBox cmb_box;
    public TextBox t_box;

    public Data(String lbl_Data,List<String> cmb_box_Data) {
        chk_box=new CheckBox();
        lbl=new Label();
        lbl.Content=lbl_Data;
        cmb_box=new ComboBox();
        cmb_box.ItemsSource=cmb_box_Data;
        chk_box.Click += new RoutedEventHandler(chk_box_clicked);
        ...
    }
    private void chk_box_clicked(object Sender, RoutedEventArgs e){
        if(chk_box.IsChecked == true) cmb_box.IsEnabled = false;
        else cmb_box.IsEnabled = true;
    }
}

现在我需要将此类的对象添加为 ListViewItem,每个控件在 ListView/GridView 中的每列 - 动态、逐行,然后在用户进行选择后再次逐行访问所有行并从中获取值控制。

XAML 代码:

<ListView x:Name="TestGrid"> <ListView.View> <GridView> <GridViewColumn Header=" Select "/> <GridViewColumn Header=" Label "/> <GridViewColumn Header=" cmb_box "/> <GridViewColumn Header=" t_box "/> </GridView> </ListView.View> </ListView>

由于所有控件的 Items Source 不同,我无法使用 ListView.ItemsSorce 绑定数据。请提出合适的选项或实现所需 UI 的方法。我想在不使用第三方 dll(如果可能的话)的情况下从 VS 中给出的控件中提取所有内容。

提前感谢您的时间和建议。

【问题讨论】:

  • 您需要重新考虑这一点。名为 Data 的类不应具有作为 UI 控件的属性。您没有将数据绑定到控件。
  • @Blam ...谢谢您的评论。无论如何,我正在将数据绑定到控件。我只是想知道该怎么做。 DataSet ds = ... //fetches all rows for a col that i need to bind //in combobox and i have a User defines List&lt;String&gt; for Label that //i pass 1 by 1. foreach(DataRow dr in ds.Table[0].Rows) cmb_list.Add(dr.ItemsArray[0]); Data temp=new Data(str_list[i],cmb_list); ListView_main.Items.Add(Data); //How to show these controls?? 上面的东西将数字循环到用户定义列表中的元素数。如果有其他方法,请建议我。
  • 不,您在 Data 中有多个 UI 控件。这是一个糟糕的设计。 UI 控件应该在数据中的 XAML 数据中。
  • @blam ...感谢您的反馈。将尝试改进设计,我明白你之前的意思。谢谢。

标签: wpf listview data-binding


【解决方案1】:

检查 msdn.microsoft.com 是否绑定

public class Datum
{
    public bool Select { get; set; }
    public string Name { get; set; }
    public List<string> Items { get; set; }
}

<ListView ItemsSource="{Binding}">
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn Header="Select">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding Path=Select, Mode=OneWay}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Name">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name, Mode= OneWay}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Name">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Path=Items}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

【讨论】:

  • @Blam...谢谢您的解决方案。将尝试实施上述解决方案并尽快回复!感谢您的回复
  • @Blam... 我修改了一些东西,现在我可以轻松显示所有需要的东西。谢谢你的建议。但是我仍然不清楚第二部分,当我必须在用户选择/编辑操作后读取数据时,我将如何访问所有这些不同控件的每一行的所有 ListViewItems 列。请给我一些建议。非常感谢。
  • 我怀疑英语不是第一语言,但你不清楚。我认为您需要 INotifyPropertyChanged。
猜你喜欢
  • 2011-07-27
  • 1970-01-01
  • 2013-12-30
  • 2012-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-30
  • 2018-03-11
相关资源
最近更新 更多