【问题标题】:WPF - ListBox ignores Style When ItemsSource is boundWPF - 绑定 ItemsSource 时 ListBox 忽略样式
【发布时间】:2010-03-16 14:42:45
【问题描述】:

我在 WPF 中创建了一个设置样式的 ListBox,以便将其呈现为复选框列表。

当我手动填充 ListBox 的项目时,样式效果很好。但是,当我将 ListBox 的 ItemsSource 绑定到静态资源(包含所需项目的 ItemsControl)时,样式将完全删除。

风格如下:

<Style x:Key="CheckBoxListStyle" TargetType="ListBox">
    <Style.Resources>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Grid Margin="2">
                            <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <CheckBox IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"/>
                            <ContentPresenter
                                Grid.Column="1"
                                Margin="2,0,0,0" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Style.Resources>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Vertical"  />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Background" Value="Transparent" />
</Style>

下面是正确显示样式的 ListBox 代码:

<ListBox x:Name="ColumnsList"
            Grid.Column="0"
            Grid.Row="0"
            Style="{StaticResource CheckBoxListStyle}"
            BorderThickness="1">                                                
            <ListBox.Items>
                <ListBoxItem>Test</ListBoxItem>
                <ListBoxItem>Test2</ListBoxItem>
                <ListBoxItem>Test3</ListBoxItem>
            </ListBox.Items>
        </ListBox>

这是忽略样式的 ListBox 的代码:

<ListBox x:Name="ColumnsList2"
            Grid.Column="0"
            Grid.Row="0"
            Style="{StaticResource CheckBoxListStyle}"
            BorderThickness="1"
            ItemsSource="{Binding Source={StaticResource Test1}, Path=Items}">
        </ListBox>

希望有人可以提供帮助 - 我对这一切都很陌生,并且已经尝试了我能想到的所有内容,但是我所阅读的所有内容都让我相信设置 ItemsSource 应该与手动设置项目具有相同的结果,所以我看不出这不起作用的任何原因。

谢谢,

【问题讨论】:

    标签: wpf xaml styling listboxitem staticresource


    【解决方案1】:

    将 Style.Resources 更改为设置 ItemContainerStyle 属性,它应该像一个魅力一样工作。

        <Style x:Key="CheckBoxListStyle" TargetType="ListBox">
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="ListBoxItem">
                    <Setter Property="Template">
                        <Setter.Value>
                                <ControlTemplate TargetType="ListBoxItem">
                                <Grid Margin="2">
                                    <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition />
                                    </Grid.ColumnDefinitions>
                                    <CheckBox IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"/>
                                    <ContentPresenter
                                        Grid.Column="1"
                                        Margin="2,0,0,0" />
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Vertical"  />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="Background" Value="Transparent" />
    </Style>
    

    在旧版本(SP1 之前)中,当您在 Style 中定义 Styles 时,其中一种样式将被忽略。或者,您可以在父资源中设置样式的资源..

    希望这会有所帮助!

    【讨论】:

    • 太棒了——除非你忘记了 Setter.Value 标签。除此之外,它似乎工作。谢谢!
    【解决方案2】:

    这是因为 CheckListBoxStyle 中的 TargetType 以 ListBoxItem 为目标,但是当您设置 ListBox 的 ItemSource 属性时,您将绑定到其他元素的列表(例如整数)。这意味着您的目标类型应该是 int 而不是 ListBoxItem。

    或者不指定目标类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-20
      • 2012-04-18
      • 1970-01-01
      • 2011-07-13
      • 2013-03-26
      • 2015-04-07
      • 2012-02-18
      相关资源
      最近更新 更多