Reputation: 2019
My question is pretty much simple as it looks. But the way I am trying to implement it is a little complex. I have implemented a Singleton pattern in order to use some global data I have class Contact History, I want to bind some of its properties to the ListView->GridView->GridViewColumn. I have a list that I want to bind. I have gone through some tutorials and tried to implement them but it seems that there are some issues with my XAML code because when I bind the listobject it can resolve its path. It seems that I am not including something right. Following is the code that would be required
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>
I am assinging the datacontext properly, but its my hunch that the xaml can't understand all the binding references that I provided I might be missing some custom class reference or something like that. I would be obliged if anyone can help me in this regard
Regards Umair
Upvotes: 1
Views: 4913
Reputation: 526
Delete binding param from listview.
Assign listview.itemssource by code. otherwise Add ObjectCollection to resources with x:Key="ContactHistoryList"
bye tiz
try this:
<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>
Upvotes: 0
Reputation: 34200
(Where are you assigning the datacontext? What are you assigning it to? )
There is no property anywhere in your code called ContactHistoryList
. That means your item source can't be binding correctly, to begin with, unless there's something involved in the data context that you're not explaining.
Also, it's not possible to bind to public fields using WPF. You need to wrap your fields in public properties and bind to those instead.
Upvotes: 1