C# ItemSource值可以计算以决定标签或单选按钮之间的样式

C# ItemSource值可以计算以决定标签或单选按钮之间的样式,c#,wpf,xaml,mvvm,data-binding,C#,Wpf,Xaml,Mvvm,Data Binding,我正在寻找一个wpf示例,当Itemsource大于1时,它可以动态创建单选按钮。如果它在Itemsource中只有一个值,那么它应该是label而不是单选按钮。我是否必须编写转换器,它可以传递Itemsource的值并控制标签和单选按钮之间的样式。如果有人能提供示例,那就太好了。您可以使用ItemContainerStyle,在ItemsControl的Items的Count属性上使用DataTrigger 样式将在两个不同的ContentTemplates之间进行选择,具体取决于是否只有一个

我正在寻找一个wpf示例,当Itemsource大于1时,它可以动态创建单选按钮。如果它在Itemsource中只有一个值,那么它应该是label而不是单选按钮。我是否必须编写转换器,它可以传递Itemsource的值并控制标签和单选按钮之间的样式。如果有人能提供示例,那就太好了。

您可以使用ItemContainerStyle,在ItemsControl的
Items
Count
属性上使用DataTrigger

样式将在两个不同的ContentTemplates之间进行选择,具体取决于是否只有一个项

<ItemsControl ItemsSource="{Binding ...}">
    <ItemsControl.Resources>
        <DataTemplate x:Key="DefaultItemTemplate">
            <RadioButton Content="{Binding ...}"/>
        </DataTemplate>
        <DataTemplate x:Key="SingleItemTemplate">
            <Label Content="{Binding ...}"/>
        </DataTemplate>
    </ItemsControl.Resources>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="ContentTemplate"
                    Value="{StaticResource DefaultItemTemplate}"/>
            <Style.Triggers>
                <DataTrigger
                    Binding="{Binding Items.Count,
                              RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                    Value="1">
                    <Setter Property="ContentTemplate"
                            Value="{StaticResource SingleItemTemplate}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>

ContentControl创建一个控件,并根据其类型为其选择正确的
数据模板。因此,您不需要转换器:

    <ContentControl Content="{Binding Child}">
        <ContentControl.Resources>
            <DataTemplate DataType="{x:Type local:Type1}">
                <Label Content="{Binding ItemText}"/>
            </DataTemplate>
            <DataTemplate DataType="{x:Type local:Type2}">
                <ItemsControl ItemsSource="{Binding Items}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <RadioButton Content="{Binding ItemText}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </DataTemplate>
        </ContentControl.Resources>
    </ContentControl>
数据类型:

public class BaseType { }
public class Type1 : BaseType
{
    public string ItemText { get; set; }
}
public class Type2 : BaseType
{
    public ObservableCollection<Type1> Items { get; } = new ObservableCollection<Type1>();
}
或者,如果您将其设置为
Type2
,您将拥有radiobutton收藏:

var t = new Type2();
t.Items.Add(new Type1 { ItemText = "hello" });
t.Items.Add(new Type1 { ItemText = "world" });
Child = t;

将contentpresenter与两个具有不同数据类型的数据模板一起使用
Child = new Type1 { ItemText = "hello" };
var t = new Type2();
t.Items.Add(new Type1 { ItemText = "hello" });
t.Items.Add(new Type1 { ItemText = "world" });
Child = t;