开发者

ItemContainerStyle overriding Generic style

I'm referencing ExpressionDark.xaml from my App.xaml, which is working fine, however when I try to use an ItemContainerStyle in an ItemsControl, the items in the ItemsControl revert to the basic styling.

<ItemsControl Grid.Column="1" VerticalAlignment="Center" Margin="10">
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Margin" Value="5" />
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.Items>
        <TextBlock Text="{Binding Error}" />
        <TextBox Text="{Binding Path=Username,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}" Validation.ErrorTemplate="{StaticResource validationTemplate}"></TextBox>
        <TextBox Text="{Binding Path=Password,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}" Validation.ErrorTemplate="{StaticResource validationTemplate}"></TextBox>
        <Button VerticalAlignment="Center" HorizontalAlignment="Right" Command="{Binding SignInCommand}" IsEnabled="{Binding CanSignIn}" Content="Sign In"></Button>
        开发者_如何转开发<TextBox Text="{Binding Path=Username}"></TextBox>
    </ItemsControl.Items>
</ItemsControl>

I'm only trying to find a good control for vertical styling (easily adding margin between items), so maybe there's a better way that doesn't override the style specified in App.xaml.

TIA


If you specify an "in place" style, it is considered as a completely new style. Due to that the default style from ExpressionDark.xaml is forgotton for that element.

What you have to do to avoid this is: Refer to the base style with BasedOn=

<ItemsControl.ItemContainerStyle>
   <Style BasedOn="{StaticResource Existing}">
      <Setter Property="Margin" Value="5" />
   </Style>
</ItemsControl.ItemContainerStyle>

Find the corresponding default style for your control. And replace Existing with the Resource-Key from ExpressionDark.xaml. You can identify it because it will have the proper TargetType property set:

<Style TargetType="{x:Type ListBoxItem}"> x:Key=...

Where ListBoxItem is your control in use (to be restyled)

You may consider using ListBox instead of ItemsControl as it has ListBoxItem as container.


They way you use the margin is fine, but when you use a different style then the one in the App.xaml, it duffently won't use the one in App.xaml.

This is the way it works in WPF, the control uses the style "closest" to it, and since you are writing this style directly into the control, it uses that style.

you can make a new style in the app.xaml with the 'BaseOn' property based on the ExpressionDark.xaml, but you'll add the:

<ItemsControl.ItemContainerStyle>
    <Style>
        <Setter Property="Margin" Value="5" />
    </Style>
</ItemsControl.ItemContainerStyle>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜