将listview子项复制到变量

时间:2014-01-24 13:18:07

标签: c# wpf listview

我已经搜索过任何解决方案,但我找不到一个。我在几个讨论我的问题的网站/论坛上发现了几个帖子,但我想要一个非常简单的解决方案。

while (SQLDataReader.Read())
                {
                    var view = new GridView();
                    view.Columns.Add(new GridViewColumn { Header = "User", DisplayMemberBinding = new Binding("User") });
                    view.Columns.Add(new GridViewColumn { Header = "hostNamedb", DisplayMemberBinding = new Binding("hostNamedb") });
                    view.Columns.Add(new GridViewColumn { Header = "fullName", DisplayMemberBinding = new Binding("fullName") });
                    view.Columns.Add(new GridViewColumn { Header = "location", DisplayMemberBinding = new Binding("location") });
                    view.Columns.Add(new GridViewColumn { Header = "department", DisplayMemberBinding = new Binding("department") });
                    view.Columns.Add(new GridViewColumn { Header = "position", DisplayMemberBinding = new Binding("postion") });
                    view.Columns.Add(new GridViewColumn { Header = "message", DisplayMemberBinding = new Binding("message") });
                    view.Columns.Add(new GridViewColumn { Header = "status", DisplayMemberBinding = new Binding("status") });
                    view.Columns.Add(new GridViewColumn { Header = "ip", DisplayMemberBinding = new Binding("ip") });

                    ListView2.View = view;
                    ListView2.Items.Add(new
                    {
                        User = SQLDataReader["User"].ToString(),
                        hostNamedb = SQLDataReader["hostNamedb"].ToString(),
                        fullName = SQLDataReader["fullName"].ToString(),
                        location = SQLDataReader["location"].ToString(),
                        department = SQLDataReader["department"].ToString(),
                        position = SQLDataReader["position"].ToString(),
                        message = SQLDataReader["message"].ToString(),
                        status = SQLDataReader["status"].ToString(),
                        ip = SQLDataReader["ip"].ToString(),
                    });
                }


                userName_click = System.Convert.ToString(ListView2.Items[var2].User.Text.ToString());
                hostName_click = System.Convert.ToString(ListView2.Items[var2].hostNamedb.Text.ToString());
                fullName_form = System.Convert.ToString(ListView2.Items[var2].SubItems[2].Text.ToString());
                location_form = System.Convert.ToString(ListView2.Items[var2].SubItems[3].Text.ToString());
                department_form = System.Convert.ToString(ListView2.Items[var2].SubItems[4].Text.ToString());
                position_form = System.Convert.ToString(ListView2.Items[var2].SubItems[5].Text.ToString());
                message_Form = System.Convert.ToString(ListView2.Items[var2].SubItems[6].Text.ToString());
                status_form = System.Convert.ToString(ListView2.Items[var2].SubItems[7].Text.ToString());
                id_form = System.Convert.ToString(ListView2.Items[var2].SubItems[8].Text.ToString());
                userIP_click = System.Convert.ToString(ListView2.Items[var2].SubItems[9].Text.ToString());

基本上,这里的代码是这样的:程序将从我的数据库中获取数据,然后将它们添加到我的Listview2。

(另外,有人可以检查我添加到listview2的项目是否有效?我现在无法运行该程序,因为我真的需要进行大量的重新编码。我正在从VB.NET迁移到C#WPF )。

继续:每个变量,如userName_click都将拥有来自的数据 listview2-row-VAR2,列为0到8。

我尝试使用绑定名称替换子项[n],例如User from - > (新绑定(“用户”))但它没有用。

顺便说一句,这是我在listview2中的XAML代码。

<ListView x:Name="ListView2" HorizontalAlignment="Left" Height="34" Margin="-10,446,-22,-50" VerticalAlignment="Top" Width="702" Grid.ColumnSpan="3" ClipToBounds="True">
            <ListView.View>
                <GridView ColumnHeaderContainerStyle="{DynamicResource CustomHeaderStyle}">
                    <l:FixedWidthColumn Header="username" DisplayMemberBinding="{Binding User}" FixedWidth="65"/>
                    <l:FixedWidthColumn Header="Host Name" DisplayMemberBinding="{Binding hostNamedb}" FixedWidth="65"/>
                    <l:FixedWidthColumn Header="fullname" DisplayMemberBinding="{Binding fullName}" FixedWidth="85"/>
                    <l:FixedWidthColumn Header="location" DisplayMemberBinding="{Binding location}" FixedWidth="65"/>
                    <l:FixedWidthColumn Header="department" DisplayMemberBinding="{Binding department}" FixedWidth="65"/>
                    <l:FixedWidthColumn Header="position" DisplayMemberBinding="{Binding position}" FixedWidth="65"/>
                    <l:FixedWidthColumn Header="message" DisplayMemberBinding="{Binding message}" FixedWidth="65"/>
                    <l:FixedWidthColumn Header="status" DisplayMemberBinding="{Binding status}" FixedWidth="65"/>
                    <l:FixedWidthColumn Header="id" DisplayMemberBinding="{Binding id}" FixedWidth="65"/>
                    <l:FixedWidthColumn Header="ip" DisplayMemberBinding="{Binding ip}" FixedWidth="65"/>

                </GridView>
            </ListView.View>
        </ListView>

1 个答案:

答案 0 :(得分:0)

你的解决方案没有关注,但既然你知道绑定我认为你已经知道了。我无法帮助您使现有代码工作,而是建议如下:使用可观察集合来保存您的数据库对象,其中包含user,hostNamedb等字段......

public DbEntry(){
    user = SQLDataReader["User"].ToString();
    hostNamedb = SQLDataReader["hostNamedb"].ToString();
    fullName = SQLDataReader["fullName"].ToString();
    location = SQLDataReader["location"].ToString();
    ...

}

这些将填充您的可观察集合。接下来,通过XAML中的ItemsSource绑定到该集合。

<ListView ItemsSource="{Binding DBObjectsCollection}">

使用数据填充变量现在只需要指定从中获取数据的“行”索引,并获取正确的字段或属性,例如:

userName_click = DBObjectsCollection[2].user;

请注意,此代码无效。这只是一个指向如何在WPF中解决问题的基本指针。关于WPF的信息已经很多了,但如果有任何问题,请随时提出问题。