WPF - 无法将样式分配给UserControl

Max*_*Max 1 .net wpf user-controls styles

如何设置带样式的UserControl的属性?(我读了相关的问题,但没有一个解决了我的问题)

我定义了一个简单的UserControl,如下所示:

<UserControl x:Class="MyProject.RedSquare"
    ...
    Height="10" Width="10" Background="Red">
    <Grid>

    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

我现在可以手动指定此控件的宽度/高度.

但我无法使用样式分配属性.

这不起作用:

<Window.Resources>
    <Style x:Key="red" TargetType="{x:Type local:RedSquare}">
        <Setter Property="Width" Value="200" />
    </Style>
</Window.Resources>

...

<local:RedSquare Style="{StaticResource red}" />
Run Code Online (Sandbox Code Playgroud)

奇怪的行为:我可以用样式修改控件的边距,但所有其他属性都不起作用?

有任何想法吗?

Col*_*inE 5

您的用户控件具有硬编码的宽度和高度:

Height="10" Width="10"
Run Code Online (Sandbox Code Playgroud)

本地属性值的优先级高于样式设置器中的值.尝试使用设计时值替换宽度/高度:

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d:DesignHeight="10" d:DesignWidth="10" 
Run Code Online (Sandbox Code Playgroud)

此致,Colin E.