wpf - 更改 ListBoxItem 样式

标签 wpf xaml

我想做这样的事情。

    <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>

但它给出了下一个错误

Cannot add content of type 'System.Windows.Controls.Border' to an object of type 'System.Object'.

以及使用它的代码

        for (int i = 0; i < 10; i++)
        {
            ListBoxItem lbItem = new ListBoxItem();
            lbItem.Content = "Item" + i;
            lb1.Add(lbItem);


        }

其中“lb1”是我的 xaml 形式的列表框

如何正确给出 ListBoxItemStyle?

最佳答案

您似乎对 XAML 的语义感到困惑。在您更加习惯 XAML 之前,将其视为 C# 的等价物可能会有所帮助。这本质上就是您现在正在做的事情:

    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 的样式内的 Setter 设置 ListBoxItem 的样式,这当然会导致某种递归问题。因此,从我们得到的代码中删除额外的样式:

    Style BorderStyle = new Style();
    Border inlineStyle = new Border { Style = BorderStyle };
    ListBoxItem item = new ListBoxItem { Style = inlineStyle };

这是无效的,因为它试图将 Style 属性(类型样式)设置为 Border 对象。这本质上是您所看到的错误的核心。

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

<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>

关于wpf - 更改 ListBoxItem 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3140550/

相关文章:

c# - 在代码后面更改图像源 - Wpf

c# - 为什么我会在 xaml 中收到“BoolToRowHeightConverter 在 Windows Presentation Foundation (WPF) 项目中不受支持的错误?

c# - WPF 数据绑定(bind)架构问题

c# - : use correct height 中的动画控件

c# - 如何通过 XAML 设置 LISTBOX 工具提示

wpf - DataContext 更改不会更新 "Attached Behavior"中的绑定(bind)

c# - 如何获取所有 USB 驱动器(已插入)

c# - 设计具有可扩展 TextBlock 的 WPF 控件

wpf - 在 Xaml 中将 TreeView 与 ContextMenu 绑定(bind)

c# - Xamarin.Forms 选项卡式页面和注销