ListBox获取选择项,加载数据并绑定
//Style
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<TextBlock Text="{Binding account}" Width="auto" Height="auto" />
//load
ListBox users =new ListBox();
users.Style = null;
uses.SelectionChagned+=(o,v)=>
{
var xuser = users.SelectedItem;
string users = xuser.GetType().GetProperty("account").GetValue(xuser).ToString();
}
//bind
using(MyDbContext db = new MyDbContext())
{
var sr = from r in db.Users.Distinct().ToList()
select new {account=r.name};
var ls = sr.Distinct().ToList();
ls.Add(new {account="所有"});
users.ItemsSource = ls;
}
</ControlTemplate>