3

我在 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 应该与手动设置项目具有相同的结果,所以我可以'看不出这不起作用的任何原因。

谢谢,

4

2 回答 2

6

将 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在 Style 中定义 s 时,其中一种样式将被忽略。或者,您可以在父资源中设置样式的资源..

希望这可以帮助!

于 2010-03-16T14:48:40.977 回答
1

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

或者,不要指定目标类型。

于 2010-03-16T14:47:39.127 回答