gpt4 book ai didi

c# - wpf 中的列表框数据绑定(bind)

转载 作者:太空狗 更新时间:2023-10-29 21:22:47 25 4
gpt4 key购买 nike

我将来自数据库的数据绑定(bind)到 ListBoxItem 的,下面是代码:

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}"/>

关于c# - wpf 中的列表框数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18547598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com