gpt4 book ai didi

WPF:如何在 ItemsControl 中使用两个不同的控件?

转载 作者:行者123 更新时间:2023-12-04 23:54:39 25 4
gpt4 key购买 nike

我有一个 WPF ItemsControl,它的 ItemsSource 绑定(bind)到 MVVM 中可观察的 View 模型集合。 ItemTemplate 设置为我想要的用户控件。但是,有些情况下我想要另一个控件而不是 XAML 中指定的控件。

我怎样才能轻松做到这一点?

最佳答案

使用 DataTemplates 将 View 模型映射到 View :

<ItemsControl ItemsSource="{Binding SomeCollectionOfViewModels}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type local:FirstViewModel}">
<Label>Foo</Label>
</DataTemplate>

<DataTemplate DataType="{x:Type local:SecondViewModel}">
<Label>Bar</Label>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>

关于WPF:如何在 ItemsControl 中使用两个不同的控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1388928/

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