WPF ListView绑定到自定义类

时间:2011-04-01 14:47:11

标签: wpf xaml listview binding

我的问题看起来非常简单。但我试图实现它的方式有点复杂。我已经实现了一个Singleton模式,以便使用一些全局数据,我有类Contact History,我想将它的一些属性绑定到ListView-> GridView-> GridViewColumn。我有一个我想绑定的列表。我已经完成了一些教程并尝试实现它们,但似乎我的XAML代码存在一些问题,因为当我绑定listobject时它可以解析它的路径。似乎我没有包括正确的东西。以下是需要的代码

Singleton Class

     class Singleton
        {
            private static Singleton instance = new Singleton();
            public List<Contacts> ContactList ;

            public SQLiteConnectionStringBuilder builder;
            public SqLiteProvider _db;
            public DataHelper _helper;
            public DataTable DataTable_Contacts;
            public DataTable DataTable_ContactHistory;
            public List<String> Contact_Names;
            public ListBox ListBox_names;
            public int Contact_Index;
            public int ContactHistory_Index;

            private Singleton()
            {
                ContactList = new List<Contacts>();

                builder = new SQLiteConnectionStringBuilder();
                builder.DataSource = Util.GetCurrentDirectory() + "TestDatabases\\DatabaseAccessLayerSqlLite.db";
                _db = new SqLiteProvider();
                _db.ConnectionString = builder.ConnectionString;
                _helper = new DataHelper(_db);
                DataTable_Contacts = new DataTable();
                DataTable_ContactHistory = new DataTable();
                Contact_Names = new List<string>();
            }
       .
       .
}

Xaml Code

<Window x:Class="NET_Data_Access_Layer_Demo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:themes="clr-namespace:WPF.Themes;assembly=WPF.Themes"
        xmlns="clr-namespace:NET_Data_Access_Layer_Demo.Properties"

        Title="Customer Contact Manager" Height="535" Width="702" Loaded="Window_Loaded" Activated="Window_Activated">
        <GroupBox Header="History" Height="230" HorizontalAlignment="Left" Margin="182,252,0,0" Name="groupBox_history" VerticalAlignment="Top" Width="487">
            <Grid>
                <Button Content="Edit" Height="23" HorizontalAlignment="Left" Margin="164,163,0,0" Name="button_edithistory" VerticalAlignment="Top" Width="75" Click="button_edithistory_Click" IsEnabled="False" />
                <Button Content="Delete" Height="23" HorizontalAlignment="Left" Margin="269,163,0,0" Name="button_deletehistory" VerticalAlignment="Top" Width="75" IsEnabled="False" Click="button_deletehistory_Click" />
                <Button Height="23" HorizontalAlignment="Left" Margin="62,163,0,0" Name="button_addhistory" VerticalAlignment="Top" Width="75" Click="button_addhistory_Click" Content="Add" IsEnabled="False" />
                <ListView IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ContactHistoryList}" Height="129" HorizontalAlignment="Left" Margin="33,19,0,0" Name="listView_history" VerticalAlignment="Top" Width="419">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Date"  Width="80" DisplayMemberBinding="{Binding ContactHistory_Date}" />
                            <GridViewColumn Header="Type"  Width="80" DisplayMemberBinding="{Binding ContactHistory_Type}" />
                            <GridViewColumn Header="Note"  Width="300" DisplayMemberBinding="{Binding ContactHistory_Note}" />
                        </GridView>
                    </ListView.View>
                </ListView>
            </Grid>
        </GroupBox>
.
.
.
</window>

我正在正确地协助datacontext,但是我的预感是xaml无法理解我提供的所有绑定引用我可能会遗漏一些自定义类引用或类似的东西。如果有人能在这方面帮助我,我将不得不承担责任

此致 Umair

2 个答案:

答案 0 :(得分:1)

(你在哪里分配datacontext?你分配给它的是什么?)

您的代码中的任何位置都没有名为ContactHistoryList的属性。这意味着您的商品来源无法正确绑定,除非数据上下文中涉及您未解释的内容。

此外,无法使用WPF绑定到公共字段。您需要将您的字段包装在公共属性中并将其绑定到那些字段。

答案 1 :(得分:0)

从列表视图中删除绑定参数。

按代码分配listview.itemssource。 除此以外 使用x:Key =“ContactHistoryList”

将ObjectCollection添加到资源

bye tiz

试试这个: <ListView IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding ContactHistoryList}"
Height="129" HorizontalAlignment="Left" Margin="33,19,0,0" Name="listView_history" VerticalAlignment="Top" Width="419">
<ListView.ItemTemplate>
<DataTemplate>
<ListViewItem>
<GridView>
<GridViewColumn Header="Date" Width="80" DisplayMemberBinding="{Binding ContactHistory_Date}" />
<GridViewColumn Header="Type" Width="80" DisplayMemberBinding="{Binding ContactHistory_Type}" />
<GridViewColumn Header="Note" Width="300" DisplayMemberBinding="{Binding ContactHistory_Note}" />
</GridView>
</ListViewItem>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>