首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >改变ListBoxItem风格

改变ListBoxItem风格
EN

Stack Overflow用户
提问于 2010-06-29 12:23:52
回答 1查看 6.2K关注 0票数 2

我想做这样的事。

代码语言:javascript
复制
    <Style TargetType="{x:Type ListBoxItem}"  >
    <Setter Property="Style">
        <Setter.Value>
                <Border Style="{StaticResource BorderStyle}" Width="200" >
                </Border>
        </Setter.Value>
    </Setter>

   </Style>
    <Style x:Key="BorderStyle" TargetType="{x:Type Border}">
    <Setter Property="Background" Value="{StaticResource BackBrush}" />
    <Setter Property="BorderBrush" Value="Black" />
    <Setter Property="BorderThickness" Value="0.5" />
    <Setter Property="CornerRadius" Value="4" />
    <Setter Property="Margin" Value="4" />
    <Setter Property="Padding" Value="4" />
   </Style>

但它会给出下一个错误

不能将'System.Windows.Controls.Border‘类型的内容添加到'System.Object’类型的对象中。

以及使用它的代码

代码语言:javascript
复制
        for (int i = 0; i < 10; i++)
        {
            ListBoxItem lbItem = new ListBoxItem();
            lbItem.Content = "Item" + i;
            lb1.Add(lbItem);


        }

其中"lb1“是xaml格式的ListBox

我怎样才能正确地给予ListBoxItemStyle?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-06-29 13:11:42

您似乎对XAML的语义感到困惑。除非您对XAML有更多的了解,否则它可能会帮助您将其看作是C#的等价物。这基本上就是你现在在做的事情:

代码语言:javascript
复制
    Style BorderStyle = new Style();
    Border inlineStyle = new Border { Style = BorderStyle };
    Style listBoxItemDefaultStyle = new Style();
    listBoxItemDefaultStyle.Setters.Add(new Setter(StyleProperty, inlineStyle));
    ListBoxItem item = new ListBoxItem { Style = listBoxItemDefaultStyle };

一个问题是,您正在从ListBoxItem的样式内为ListBoxItem设置一个样式,这当然会导致递归出现某种问题。因此,从我们得到的代码中删除额外的样式:

代码语言:javascript
复制
    Style BorderStyle = new Style();
    Border inlineStyle = new Border { Style = BorderStyle };
    ListBoxItem item = new ListBoxItem { Style = inlineStyle };

这是无效的,因为它试图将样式属性(类型样式)设置为边框对象。这基本上就是你所看到的错误的核心所在。

在这种情况下,您真正想要的是更改ListBoxItem ControlTemplate以合并您的边界样式。这是用于使用您的边框而不是使用TemplateBindings设置其属性的标准样式的默认样式:

代码语言:javascript
复制
<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="Padding" Value="2,0,0,0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border x:Name="Bd" Style="{StaticResource BorderStyle}" Width="200">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="true"/>
                            <Condition Property="Selector.IsSelectionActive" Value="false"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3140550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档