Frage

I have a ListBoxItem template like below

<Window.Resources>
    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="Padding"
                Value="20" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border x:Name="Bd"
                            Padding="{TemplateBinding Padding}">
                        <ContentPresenter />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

In above sample Padding is 20 by default. So the Border control named Bd also gets the same value. So above resource will replace all the ListboxIem of ListBox in my window with the defined one. I have a specific ListBox which contains a ToggleButton also get the Padding value 20. My requirement is to make Padding value to 10 for this specific ListBox border. Could anyone please help me on this. Thanks in advance

<ListBox Name="lb">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <ToggleButton IsChecked="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}">
                <TextBlock x:Name="txt">
                        </TextBlock>
            </ToggleButton>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
War es hilfreich?

Lösung

You can define a new style for your listbox that will be based on the original style but overrides the Padding property. This way, the style you set explicitly for the listbox will be used instead of the one defined in the window's resources:

<Window>
    <Window.Resources>
        <Style x:Key="listBoxItemStyle" TargetType="{x:Type ListBoxItem}">
            <Setter Property="Padding" Value="20" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border x:Name="Bd"
                                Padding="{TemplateBinding Padding}">
                            <ContentPresenter />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource listBoxItemStyle}">
    </Window.Resources>

    <ListBox Name="lb">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource listBoxItemStyle}">
                <Setter Property="Padding" Value="10" />
            </Style>
        </ListBox.ItemContainerStyle>

        <ListBox.ItemTemplate>
            <DataTemplate>
                <ToggleButton IsChecked="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}">
                    <TextBlock x:Name="txt">
                    </TextBlock>
                </ToggleButton>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Window>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top