...
코드 뒤에 (보기 모델)
public class ProductCategoryViewModel : ViewModelBase
{
private ObservableCollection<ProductCategory> _productCategories;
public ObservableCollection<ProductCategory> ProductCategories
{
get => _productCategories;
set => Set(ref _productCategories, value);
}
public RelayCommand<string> ClickCategoryCommand { get; set; }
public ProductCategoryViewModel()
{
_productCategories = new ObservableCollection<ProductCategory>();
var p1 = new ProductCategory()
{
Description = "P1",
ChildProductCategories = new List<ProductCategory>()
{
new ProductCategory()
{
Description = "C1",
ChildProductCategories = new List<ProductCategory>()
{
new ProductCategory()
{
Description = "C1 C1"
},
new ProductCategory()
{
Description = "C1 C2"
}
}
},
new ProductCategory()
{
Description = "C2"
}
}
};
_productCategories.Add(p1);
ClickCategoryCommand = new RelayCommand<string>(Click);
}
private void Click(string description)
{
MessageBox.Show(description);
}
}
XAML 코드
<TreeView ItemsSource="{Binding ProductCategories}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:ProductCategory}" ItemsSource="{Binding ChildProductCategories}">
<Button Content="{Binding Path=Description}"
Command="{Binding DataContext.ClickCategoryCommand, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}}"
CommandParameter="{Binding Description}"/>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
귀하의 질문에 무엇입니까 ? – Fruchtzwerg
@Fruchtzwerg 어떻게 바인딩을 만들 수 있습니까 비슷한 주제를 찾았지만 결정적인 숫자와 자식 개체의 유형에 대해 이야기합니다. –
@Fruchtzwerg 여기에서 여러분은 관계가 1 대 다수 일 수 있으므로 부모 객체는 자식과 동일한 유형이므로 일부 재귀가 필요할 수도 있습니다. –