1

App.xaml から ExpressionDark.xaml を参照していますが、正常に動作していますが、ItemsControl で ItemContainerStyle を使用しようとすると、ItemsControl の項目が基本的なスタイルに戻ります。

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

私は、垂直方向のスタイリング (アイテム間にマージンを簡単に追加する) の適切なコントロールを見つけようとしているだけなので、App.xaml で指定されたスタイルをオーバーライドしないより良い方法があるかもしれません。

ティア

4

2 に答える 2

2

「 in place 」スタイルを指定すると、まったく新しいスタイルと見なされます。そのため、その要素のExpressionDark.xamlの既定のスタイルは忘れられています。

これを避けるためにあなたがしなければならないことは次のとおりです: BasedOn= で基本スタイルを参照してください

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

コントロールに対応する既定のスタイルを見つけます。ExpressionDark.xamlExistingの Resource-Key に置き換えます。適切なプロパティ セットがあるため、識別できます。TargetType

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

ListBoxItem は使用中のコントロールです (スタイルを変更する)

コンテナとしてそのままではListBoxなく、使用を検討することもできます。ItemsControlListBoxItem

于 2014-06-20T07:56:26.580 に答える
0

マージンの使用方法は問題ありませんが、 のスタイルとは異なるスタイルを使用すると、 のスタイルはApp.xaml使用されませんApp.xaml

これは WPF での動作方法であり、コントロールはそれに「最も近い」スタイルを使用し、このスタイルをコントロールに直接書き込んでいるため、そのスタイルを使用します。

app.xamlに基づく 'BaseOn' プロパティを使用して で新しいスタイルを作成できますがExpressionDark.xaml、以下を追加します。

<ItemsControl.ItemContainerStyle>
    <Style>
        <Setter Property="Margin" Value="5" />
    </Style>
</ItemsControl.ItemContainerStyle>
于 2011-01-05T23:14:50.963 に答える