I am binding the data coming form database to ListBoxItem's
, below is the code:
public void load_users()
{
RST_DBDataContext conn = new RST_DBDataContext();
List<TblUser> users = (from s in conn.TblUsers
select s).ToList();
Login_Names.ItemsSource = users;
}
And in XAML, there is the below code:
<ListBox Name="Login_Names"
ItemsSource="{Binding Path=UserName}"
HorizontalAlignment="Left"
Height="337" Margin="10,47,0,0"
Padding="0,0,0,0" VerticalAlignment="Top" Width="156">
But it does not works, it shows table name, but I need to see the usernames coming from table, there is column called UserName in TblUsers.
Thanks in Advance.
Data binding in Windows Presentation Foundation (WPF) provides a simple and consistent way for apps to present and interact with data. Elements can be bound to data from different kinds of data sources in the form of . NET objects and XML.
ListBox is a control that provides a list of items to the user item selection. A user can select one or more items from the predefined list of items at a time. In a ListBox, multiple options are always visible to the user without any user interaction.
ItemsSource can be data bound to any sequence that implements the IEnumerable interface, although the type of collection used does determine the way in which the control is updated when items are added to or removed. When ItemsSource is set, the Items property cannot be used to control the displayed values.
The TextBlock control provides flexible text support for UI scenarios that do not require more than one paragraph of text. It supports a number of properties that enable precise control of presentation, such as FontFamily, FontSize, FontWeight, TextEffects, and TextWrapping.
try this
Create DataTemplate in resource section and then assign it to listbox
<Grid.Resources>
<DataTemplate x:Key="userNameTemplate">
<TextBlock Text="{Binding Path=UserName}"/>
</DataTemplate>
<ListBox Name="listBox" ItemsSource="{Binding}"
ItemTemplate="{StaticResource userNameTemplate}"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With