小编Max*_*Max的帖子

WPF - 无法将样式分配给UserControl

如何设置带样式的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)

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

有任何想法吗?

.net wpf user-controls styles

1
推荐指数
1
解决办法
210
查看次数

标签 统计

.net ×1

styles ×1

user-controls ×1

wpf ×1