すべての要素に適用されるスタイルを指定できますか?私は試した
<Style TargetType="Control">
<Setter Property="Margin" Value="0,5" />
</Style>
しかし、それは何もしませんでした
回答:
Styleあなただけのターゲットにされて作成されたControlから派生要素をしていませんControl。設定しない場合x:KeyはTargetType、暗黙的にに設定されるため、あなたの場合はx:Key="{x:Type Control}"。
指定する任意の直接的な方法はありませんStyleから派生するすべての要素ターゲットTargetTypeのはStyle。他にもいくつかのオプションがあります。
次の場合 Style
<Style x:Key="ControlBaseStyle" TargetType="{x:Type Control}">
<Setter Property="Margin" Value="50" />
</Style>
Buttonsたとえば、すべてをターゲットにすることができます
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource ControlBaseStyle}"/>
または、任意の要素に直接スタイルを使用します。 Button
<Button Style="{StaticResource ControlBaseStyle}" ...>
Fredrik Hedbladが答えたように、コントロールから継承したすべての要素に影響を与えることができます。
ただし、たとえば同じスタイルのテキストブロックとボタンにスタイルを適用することはできません。
それを行うには:
<Style x:Key="DefaultStyle" TargetType="{x:Type FrameworkElement}">
<Setter Property="Control.Margin" Value="50"/>
</Style>
<Style TargetType="TextBlock" BasedOn="{StaticResource DefaultStyle}"/>
<Style TargetType="Button" BasedOn="{StaticResource DefaultStyle}"/>
FrameworkElementすべてのコントロールに適用されていないのはなぜだろうと思っていました。これでその質問に答えました。