[英]Listbox Databinding in wpf
我將數據庫中的數據綁定到ListBoxItem's
,下面是代碼:
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;
}
在XAML中,有以下代碼:
<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">
但它不起作用,它顯示表名,但我需要看到來自表的用戶名,在TblUsers中有一個名為UserName的列。
提前致謝。
試試這個
在資源部分中創建DataTemplate,然后將其分配給列表框
<Grid.Resources>
<DataTemplate x:Key="userNameTemplate">
<TextBlock Text="{Binding Path=UserName}"/>
</DataTemplate>
<ListBox Name="listBox" ItemsSource="{Binding}"
ItemTemplate="{StaticResource userNameTemplate}"/>
由於ItemsSource已經在代碼中設置,因此在XAML中將DisplayMemberPath設置為UserName。
<ListBox Name="Login_Names"
DisplayMemberPath="UserName"
HorizontalAlignment="Left"
Height="337" Margin="10,47,0,0"
Padding="0,0,0,0" VerticalAlignment="Top" Width="156">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.