需要多种样式取决于Listboxitem [英] Need multiple styles dependent on Listboxitem

查看:57
本文介绍了需要多种样式取决于Listboxitem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框,该列表框基于相同的基类存储两种不同的对象类型. (例如,BaseObject = baseclass及其子级:CustomPath和CustomImage)

数据源:

ObservableCollection<BattlegroundBaseObject> _baseObjectCollection;
public ObservableCollection<BattlegroundBaseObject> BaseObjectCollection
{
    get { return _baseObjectCollection?? (_baseObjectCollection= new ObservableCollection<BaseObject>()); }
} 

列表框数据绑定:<ListBox ItemsSource="{Binding BaseObjectCollection}"

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem" x:Name="ListBoxPathLineStyle">
        <Setter Property="Template">

            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem" x:Name="BattlegroundObjectControlTemplate">

                    <Path Stroke="{Binding ObjectColor}" StrokeThickness="{Binding StrokeThickness}" Data="{Binding PathGeometryData}" x:Name="PathLine" Opacity="{Binding Opacity}">
                    </Path>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="Effect" TargetName="PathLine">
                                <Setter.Value>
                                    <DropShadowEffect Color="CornflowerBlue" ShadowDepth="3" BlurRadius="10" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ListBox.ItemContainerStyle>

我想添加到路径所在的ControlTemplate,也是一个Image,并按类型或属性来区别它.没关系.

任何人有什么想法吗?

解决方案

您可以将每种类型的资源添加到ListBox资源DataTemplate.

在我的示例中,从Vehicle类派生的类CarMotorbike.

<ListBox x:Name="listBox">
    <ListBox.Resources>
        <DataTemplate DataType="{x:Type local:Car}">
            <StackPanel Background="Red">
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>

        <DataTemplate DataType="{x:Type local:Motorbike}">
            <StackPanel Background="Orange">
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.Resources>
</ListBox>

您可以为资源添加ListBoxItem样式:

<ListBox x:Name="listBox">
    <ListBox.Resources>
        <Style TargetType="ListBoxItem">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="true">
                    <Setter Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect Color="CornflowerBlue" ShadowDepth="3" BlurRadius="10" />
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
        <DataTemplate DataType="{x:Type local:Car}">
            <StackPanel Background="Red">                       
                <TextBlock Text="{Binding Name}" />                        
            </StackPanel>
        </DataTemplate>

        <DataTemplate DataType="{x:Type local:Motorbike}">
            <StackPanel Background="Orange">
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.Resources>
</ListBox>

i have a Listbox, which stores two different object types, based on the same baseclass. (e.g. BaseObject = baseclass and the children of it: CustomPath and CustomImage)

The Datasource:

ObservableCollection<BattlegroundBaseObject> _baseObjectCollection;
public ObservableCollection<BattlegroundBaseObject> BaseObjectCollection
{
    get { return _baseObjectCollection?? (_baseObjectCollection= new ObservableCollection<BaseObject>()); }
} 

The Listbox databinding: <ListBox ItemsSource="{Binding BaseObjectCollection}"

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem" x:Name="ListBoxPathLineStyle">
        <Setter Property="Template">

            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem" x:Name="BattlegroundObjectControlTemplate">

                    <Path Stroke="{Binding ObjectColor}" StrokeThickness="{Binding StrokeThickness}" Data="{Binding PathGeometryData}" x:Name="PathLine" Opacity="{Binding Opacity}">
                    </Path>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="Effect" TargetName="PathLine">
                                <Setter.Value>
                                    <DropShadowEffect Color="CornflowerBlue" ShadowDepth="3" BlurRadius="10" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ListBox.ItemContainerStyle>

I want to add to the ControlTemplate where the Path is, also a Image and to differ it by type or a property. doesnt matter.

anyone any ideas?

解决方案

You can add to ListBox resources DataTemplate for each type.

In my example classes Car and Motorbike derived from Vehicle class.

<ListBox x:Name="listBox">
    <ListBox.Resources>
        <DataTemplate DataType="{x:Type local:Car}">
            <StackPanel Background="Red">
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>

        <DataTemplate DataType="{x:Type local:Motorbike}">
            <StackPanel Background="Orange">
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.Resources>
</ListBox>

EDIT:

You can add style for ListBoxItem to resources:

<ListBox x:Name="listBox">
    <ListBox.Resources>
        <Style TargetType="ListBoxItem">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="true">
                    <Setter Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect Color="CornflowerBlue" ShadowDepth="3" BlurRadius="10" />
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
        <DataTemplate DataType="{x:Type local:Car}">
            <StackPanel Background="Red">                       
                <TextBlock Text="{Binding Name}" />                        
            </StackPanel>
        </DataTemplate>

        <DataTemplate DataType="{x:Type local:Motorbike}">
            <StackPanel Background="Orange">
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.Resources>
</ListBox>

这篇关于需要多种样式取决于Listboxitem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆