gbzhhu
asked on
How do I do databinding in WPF - ListBox and ListView
Hi,
I am using VS 2008 Beta 2 on Vista. I am finding databinding on WPF a little difficult. I have seen lots of examples of people binding to textboxes. Basically I have a table users with a few columns - firstName, lastName, email. I have a stored procedure that returns a list of users. I have created a SqlDataAdapter that gets data and fills a DataTable dt. I have a a listview lvUsers. Now how do I bind the data in dt to lvUsers? Used to be easy as control.DataSource = dt
Thanks
I am using VS 2008 Beta 2 on Vista. I am finding databinding on WPF a little difficult. I have seen lots of examples of people binding to textboxes. Basically I have a table users with a few columns - firstName, lastName, email. I have a stored procedure that returns a list of users. I have created a SqlDataAdapter that gets data and fills a DataTable dt. I have a a listview lvUsers. Now how do I bind the data in dt to lvUsers? Used to be easy as control.DataSource = dt
Thanks
ASKER
Hi Bob,
I thought only you would be the one attempting to answer this :-)
I have seen that article and numerous others like these.
http://blogs.msdn.com/henryh/archive/2005/06/23/431846.aspx
http://msdn.microsoft.com/msdnmag/issues/07/12/DataPoints/
I have now managed to databind the ListBox. It is displaying fields vertically, i.e
Row1
field 1
field 2
field 3
Row 2
field 1
field 2
field 3
And I have no styling. Having used to doing the styling via property pages and also creating templates via GUI, I am now wondering why we have to code GUI in XAML which is simple looking language but certainly not easy.
What I was attempting to do was load a list of users from DB and show them in List control (ListBox, ListView etc) then user types a few characters on textbox I use for filtering then I would run some LINQ to get users whose names start with the characters entered. all works apart from styling (and when LINQ is applied to filter users I get the right number of users back but the ListBox although it contains the correct number of users does not display them - there must some tweaking I am missing). All in all it is weird way of doing things
I am just looking at the web for a WPF book. It is said that every Windows Forms developer must learn WPF so better start now!
Hassan
I thought only you would be the one attempting to answer this :-)
I have seen that article and numerous others like these.
http://blogs.msdn.com/henryh/archive/2005/06/23/431846.aspx
http://msdn.microsoft.com/msdnmag/issues/07/12/DataPoints/
I have now managed to databind the ListBox. It is displaying fields vertically, i.e
Row1
field 1
field 2
field 3
Row 2
field 1
field 2
field 3
And I have no styling. Having used to doing the styling via property pages and also creating templates via GUI, I am now wondering why we have to code GUI in XAML which is simple looking language but certainly not easy.
What I was attempting to do was load a list of users from DB and show them in List control (ListBox, ListView etc) then user types a few characters on textbox I use for filtering then I would run some LINQ to get users whose names start with the characters entered. all works apart from styling (and when LINQ is applied to filter users I get the right number of users back but the ListBox although it contains the correct number of users does not display them - there must some tweaking I am missing). All in all it is weird way of doing things
I am just looking at the web for a WPF book. It is said that every Windows Forms developer must learn WPF so better start now!
Hassan
XAML is a completely different beast than anything that I have seen in a long time. Have you seen or used anything like Expression?
What kind of styling are you looking for?
Bob
What kind of styling are you looking for?
Bob
ASKER
Yeah I agree about XAML! I guess it is a matter of getting used to it. I prefer object based priogramming as opposed to declarative.
Do you mean Expression Blend? I haven't used it. Does it come with VS 2008 (I am using VS 2008 Beta 2)?
Styling wise I am after 2 types of styling, basic which I should be able to achieve at some point and fancy which may not be possible
Basic
- display header row with column header text
- display fields as columns horizontally like a datagrid would
Fancy (as above plus)
- header bgcolour - gradient
- borders and gradient colour on selected rows
That'd do for now :-)
Do you mean Expression Blend? I haven't used it. Does it come with VS 2008 (I am using VS 2008 Beta 2)?
Styling wise I am after 2 types of styling, basic which I should be able to achieve at some point and fancy which may not be possible
Basic
- display header row with column header text
- display fields as columns horizontally like a datagrid would
Fancy (as above plus)
- header bgcolour - gradient
- borders and gradient colour on selected rows
That'd do for now :-)
Yeah, Expression Blend ;)
It is a separate application from VS 2008, but comes in the MSDN subscription. It makes it easier to style XAML.
How is your ListBox declared?
Bob
It is a separate application from VS 2008, but comes in the MSDN subscription. It makes it easier to style XAML.
How is your ListBox declared?
Bob
ASKER
I see. We only received Beta 2 and we are waiting for the RTM stuff. I will install Expression Blend then and hope it helps.
My whole XAML below
<Window x:Class="WpfApp1.Users"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Users" Height="402" Width="495">
<Grid>
<Button Height="25" HorizontalAlignment="Left" Margin="18,0,0,24" Name="btnUsers" VerticalAlignment="Bottom" Width="109" Click="btnUsers_Click">Get Users</Button>
<TextBox Height="25" Margin="140,0,94,25" Name="txtUser" VerticalAlignment="Bottom" />
<ListBox Margin="10,10,12,66" Name="listBox1" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=firstName}" />
<TextBlock Text="{Binding Path=lastName}"/>
<TextBlock Text="{Binding Path=email}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock Height="20" HorizontalAlignment="Left" Margin="26,0,0,3" Name="textBlock1" VerticalAlignment="Bottom" Width="104" />
<TextBox Height="20" Margin="140,0,188,2" Name="textBox1" VerticalAlignment="Bottom" />
</Grid>
</Window>
And my whole code behind minus using directives
public partial class Users : Window
{
DataTable dt = new DataTable();
public Users()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Users_L oaded);
}
void Users_Loaded(object sender, RoutedEventArgs e)
{
SqlDataAdapter adaptor = new SqlDataAdapter("cms_getUse rs", "server=dbserver;database= bayer_bios cience;use r id=webBayer;password=passw ord;");
adaptor.Fill(dt);
DataContext = dt;
//this.listView1.DataConte xt = ds.Tables[0];
//Binding bind = new Binding();
//bind.Source = ds.Tables[0];
//this.listView1.SetBindin g(ListView .ItemsSour ceProperty , bind);
}
private void btnUsers_Click(object sender, RoutedEventArgs e)
{
var query = from r in dt.AsEnumerable()
where r.Field<string>("lastName" ).StartsWi th(this.tx tUser.Text )
select r;
//DataContext = query;
//listBox1.DataContext = DataContext;
}
}
My whole XAML below
<Window x:Class="WpfApp1.Users"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Users" Height="402" Width="495">
<Grid>
<Button Height="25" HorizontalAlignment="Left"
<TextBox Height="25" Margin="140,0,94,25" Name="txtUser" VerticalAlignment="Bottom"
<ListBox Margin="10,10,12,66" Name="listBox1" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=firstName}" />
<TextBlock Text="{Binding Path=lastName}"/>
<TextBlock Text="{Binding Path=email}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock Height="20" HorizontalAlignment="Left"
<TextBox Height="20" Margin="140,0,188,2" Name="textBox1" VerticalAlignment="Bottom"
</Grid>
</Window>
And my whole code behind minus using directives
public partial class Users : Window
{
DataTable dt = new DataTable();
public Users()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Users_L
}
void Users_Loaded(object sender, RoutedEventArgs e)
{
SqlDataAdapter adaptor = new SqlDataAdapter("cms_getUse
adaptor.Fill(dt);
DataContext = dt;
//this.listView1.DataConte
//Binding bind = new Binding();
//bind.Source = ds.Tables[0];
//this.listView1.SetBindin
}
private void btnUsers_Click(object sender, RoutedEventArgs e)
{
var query = from r in dt.AsEnumerable()
where r.Field<string>("lastName"
select r;
//DataContext = query;
//listBox1.DataContext = DataContext;
}
}
ASKER
Bob,
Are you any good at Mobile dev? Windows Mobile 5 and CF 2.0?
I have this issue that I couldn't fix for 5 months now !!!!
Are you any good at Mobile dev? Windows Mobile 5 and CF 2.0?
I have this issue that I couldn't fix for 5 months now !!!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>I have this issue that I couldn't fix for 5 months now !!!!
Is there a question open?
Bob
Is there a question open?
Bob
ASKER
I had a question open but it is now closed - I'll explain.
Basically this issue doesn't stop program from running. Just that when an exception occurs I get a generic error explained here
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2460541&SiteID=1
I can open a question if you could look into it or find others whop are good at this topic. I only know of one Mikal613 but he/she could not fix this one.
by the way, tyhe gradient colour you showed me is brilliant! thanks
Basically this issue doesn't stop program from running. Just that when an exception occurs I get a generic error explained here
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2460541&SiteID=1
I can open a question if you could look into it or find others whop are good at this topic. I only know of one Mikal613 but he/she could not fix this one.
by the way, tyhe gradient colour you showed me is brilliant! thanks
You would have to open another question, and give me the URL for it before I could help you.
Bob
Bob
ASKER
Sorry Bob. left this open too long. I have abandoned my WPF research for now. I bought a book and want to read it first then get back to it. Thanks for helping as always
H
H
Have you seen articles like this?
WPF Data Binding Tutorial
http://coredotnet.blogspot.com/2006/05/wpf-data-binding-tutorial.html
Bob