我已经创建风格在WPF列表框,以便它呈现为一个复选框列表。

当我手动填充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 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 x:Name="ColumnsList2"
            Grid.Column="0"
            Grid.Row="0"
            Style="{StaticResource CheckBoxListStyle}"
            BorderThickness="1"
            ItemsSource="{Binding Source={StaticResource Test1}, Path=Items}">
        </ListBox>

希望有人能帮助 - 我很新的这一切,并已尝试一切我能想到的,但一切我读过使我相信,设置的ItemsSource应该有相同的结果作为手动设置的项目,所以我看不出有任何理由为什么这是行不通的。

谢谢,

AT

有帮助吗?

解决方案

更改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)之前的旧版本,当你的风格定义Styles,这些风格的人会被忽略。可替代地,可以设置样式的资源在父资源..

希望这有助于!

其他提示

这是因为你在CheckListBoxStyle TargetType的被靶向一个ListBoxItem的,但在设置列表框的属性的ItemSource要绑定到其他元素(整数为例)的列表。这意味着你的目标类型应是int代替ListBoxItem中。

可替换地不指定目标类型。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top