WPF - ListBox ignores style when ItemsSource is bound

I created a ListBox style in WPF so that it appears as a list of checkboxes.

When I fill in the ListBox items manually, styling works fine. However, when I instead bind the ItemSource ListBox to a static resource (ItemsControl containing the required items), the styling is completely removed.

Here's the style:

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

Here is the code for the ListBox that shows the style correctly:

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

Here is the code for the ListBox that ignores the style:

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

Hoping someone can help - I'm pretty new to all this and have tried everything I can come up with, but everything I read leads me to think that setting the ItemsSource should have the same result as setting the items manually , so I see no reason why this will not work.

Thank,

at

+3
2

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

, !

+6

, TargetType CheckListBoxStyle ListBoxItem, ItemSource ListBox, (, ints). , int ListBoxItem.

.

+1

Source: https://habr.com/ru/post/1737163/


All Articles