gpt4 book ai didi

c# - 我如何在另一个 ItemsControl 中绑定(bind)父 ItemsControl?

转载 作者:行者123 更新时间:2023-12-03 10:31:08 30 4
gpt4 key购买 nike

我正在使用带有 MVVM 模式的 Wpf,所以在 Xamel 中,我有一个 itemControl 持有另一个 itemcontrol,每个 itemcontrol 绑定(bind)来自不同的 ObservableCollection,结果它应该像对每个数据进行分组一样持有数据的冲击力(类别和子类别),所以我从我的 View 模型绑定(bind)用户控件等等这是我的 View 模型代码

public class BudgetViewModel:ViewModelBase,IBudgetViewModel
{
public BudgetViewModel(IBudgetLookupsDataService BudgetLookupsDataService)
{
CategoryBudgets = new ObservableCollection<CategoryBudgetItemsViewModel>();
SubCategoryBudgets = new ObservableCollection<SubCategoryBudgetItemsViewModel>();
}
public async Task LoadBudgetCategories()
{
var categories = await _budgetLookupsDataService.GetBugetCategory();

CategoryBudgets.Clear();
if(categories.Count()!=0)
{
foreach (var cat in categories)
{
var subcategory = await _budgetLookupsDataService
.GetBudgetSubCategoriesPerMounth(DateforSearch, cat.IdCategory);
CategoryBudgets.Add(new CategoryBudgetItemsViewModel(cat.IdCategory,
cat.CategoryName));
foreach (var sub in subcategory)
{

SubCategoryBudgets.Add(new SubCategoryBudgetItemsViewModel(sub.IdSubCategory,
sub.SubCategoryName, sub.BudgetId, sub.BudgetPerMonth));
}

}
}
}
}
这是可观察对象中的类
public class CategoryBudgetItemsViewModel:ViewModelBase
{
public CategoryBudgetItemsViewModel(int categoryId,string categoryName)
{
CategoriesId = categoryId;
CategoriesName = categoryName;
}
public int CategoriesId { get; set; }

public string CategoriesName
{
get { return _categoriesName; }
set
{

_categoriesName = value;
OnPropertyChanged();

}
}
}
和下一类下一个可观察的
 public class SubCategoryBudgetItemsViewModel: ViewModelBase
{
public SubCategoryBudgetItemsViewModel(int subcategoryId, string subcategoryname,
int? idBudget, decimal budgetAmount)
{
SubCategoryId = subcategoryId;
SubCategoryName = subcategoryname;
BudgetId = idBudget;
BudgetAmount = budgetAmount;
}
public int SubCategoryId { get; set; }
public int? BudgetId { get; set; }
public string SubCategoryName
{
get { return _subcategoryname; }
set { _subcategoryname = value; OnPropertyChanged(); }
}

public decimal BudgetAmount
{
get { return _budgetamount; }
set
{
_budgetamount = value;
OnPropertyChanged();
}
}
}
和我的 Xamel 代码:
       <ItemsControl x:Name="Item"  ItemsSource="{Binding CategoryBudgets}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid x:Name="GrdData" >
<Expander Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2"
Header="{Binding CategoriesName}" Cursor="Hand" IsExpanded="True">
<ItemsControl ItemsSource="{Binding SubCategoryBudgets}" >
<ItemsControl.ItemTemplate>
<DataTemplate>

<TextBlock Text="{Binding SubCategoryId}"/>

</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Expander>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
当我运行 Expander 渲染并且绑定(bind)工作正常但另一个 itemcontrol 它根本不起作用
所以我的问题是为什么内部 itemcontrol 不起作用,谢谢大家

最佳答案

Finlay 子项控件像父项一样工作得很好:),我做了一些小改动,这是 CategoryBudgetItemsViewModel 中的更改

public class CategoryBudgetItemsViewModel : ViewModelBase
{
public CategoryBudgetItemsViewModel(int categoryId, string categoryName)
{
CategoriesId = categoryId;
CategoriesName = categoryName;
SubCategoryBudgets = new ObservableCollection<SubCategoryBudgetItemsViewModel>();
}
public int CategoriesId { get; set; }

public ObservableCollection<SubCategoryBudgetItemsViewModel> SubCategoryBudgets { get; set; }

public string CategoriesName
{
get { return _categoriesName; }
set
{

_categoriesName = value;
OnPropertyChanged();

}
}
}
我在 CategoryBudgetItemsViewModel 中填充了 SubCategoryBudgets。这是 Xamel 代码:
<ItemsControl x:Name="Item"  ItemsSource="{Binding CategoryBudgets}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid x:Name="GrdData" >
<Expander Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2"
Header="{Binding CategoriesName}" Cursor="Hand" IsExpanded="True">
<ItemsControl ItemsSource="{Binding Path=SubCategoryBudgets}"
<ItemsControl.ItemTemplate>
<DataTemplate>

<TextBlock Text="{Binding Path=SubCategoryId}/>

</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Expander>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
感谢@Clemens、@user1672994 和@icode 为我提供的帮助,
谢谢你们所有人

关于c# - 我如何在另一个 ItemsControl 中绑定(bind)父 ItemsControl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65310437/

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