Listbox Databinding in wpf

14,625

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}"/>
Share:
14,625

Related videos on Youtube

Ahmad Gulzar
Author by

Ahmad Gulzar

A Killing Programmer

Updated on September 26, 2022

Comments

  • Ahmad Gulzar
    Ahmad Gulzar over 1 year

    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.