代码之家  ›  专栏  ›  技术社区  ›  Polaris

更改ListBoxItem样式

  •  2
  • Polaris  · 技术社区  · 14 年前

        <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”类型的对象。

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

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

    如何正确地给出ListBoxItemStyle?

    1 回复  |  直到 14 年前
        1
  •  6
  •   John Bowen    14 年前

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

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

    这是无效的,因为它正在尝试将Style属性(Style类型)设置为Border对象。这就是你所看到的错误的核心所在。

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