gpt4 book ai didi

c# - 带有 WCF 服务列表的数据绑定(bind)组合框

转载 作者:行者123 更新时间:2023-12-03 10:28:44 24 4
gpt4 key购买 nike

我正在为使用 WCF 服务从数据库接收数据的 Windows 商店开发应用程序(MVVM 模式)。
我想将类别列表数据绑定(bind)到组合框中,但它对我不起作用,我搜索了网络但仍然没有找到解决方案。

类(class)类别:

   public Category(Category c)
{
this.Id=c.Id;
this.Name = c.Name;

}
public int Id { get; set; }
public string Name { get; set; }

xml:
 <ComboBox x:Name="ChooseCategory"
ItemsSource="{Binding ListCategories}"
DisplayMemberPath="Name"
SelectedValuePath="Id"
SelectedValue="{Binding SelectedItem, Mode=TwoWay}"/>

View 模型:
public ObservableCollection<Category> ListCategories { get; private set; }

在 OnNavigatedTo 函数中:
   var listCategory = await proxy.GetAllCategoriesAsync();
List<Category> list = new List<Category>();
foreach (var item in listCategory)
{

list.Add(new Category(item));
}
ListCategories = new ObservableCollection<Category>(list);

任何人???

最佳答案

您需要执行 INotifyPropertyChanged为了让 UI 知道您更改了 ListCategories收藏。

在您的 ViewModel 中,实现接口(interface) INotifyPropertyChanged

public class YourViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}

private ObservableCollection<Category> _categories;
public ObservableCollection<Category> ListCategories
{
get { return _categories; }
set
{
if (_categories != value)
{
_categories = value;
OnPropertyChanged("ListCategories");
}
}
}

关于c# - 带有 WCF 服务列表的数据绑定(bind)组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20793405/

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