问题与WPF用户控件应用样式 [英] Issue with applying style on WPF UserControl

查看:212
本文介绍了问题与WPF用户控件应用样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控制,我想在其他一些项目中使用它。有没有问题,当我直接设置一些值其属性:

I have a user-control and I want to use it in some other project. There is no problem when I set some value to its properties directly:

<local:MyUserControl prop1="val1" prop2="val2">
    ...
</local:MyUserControl>



但我不能一个样式应用到它。我想:

But I can't apply a style to it. I tried:

<Window ...>
    <Window.Resources>
        <Style x:Key="MyUserControlStyle" TargetType="{x:Type local:MyUserControl}">
            <Setter Property="prop1" Value="val1"/>
            <Setter Property="prop2" Value="val2"/>
        </Style>
    </Window.Resources>

    <Grid>
        <local:MyUserControl Style="{StaticResource ResourceKey=MyUserControlStyle}">
            ...
        </local:MyUserControl>
    </Grid>
</Window>



在哪里做我错了吗? - 谢谢

Where did I wrong? -Thanks

推荐答案

使用亲爱@Mario Vernari的指示,我发现,这个问题是由于一个糟糕的战略,我ð用于创建我的用户。我想创建一个用户控件是可以举办一些其他的。所以,我曾试图这样的:

Using dear @Mario Vernari's instructions, I found it out that the problem was due to a bad strategy which I'd used to create my UserControl. I wanted to create a UserControl that be able to hold some other ones. So I had tried this:

<UserControl x:Class="MyNamespace.MyUserControl"
             ...
             Style="{DynamicResource ResourceKey=MyUserControlStyle}">
    <UserControl.Resources>
        ...
        <Style x:Key="MyUserControlStyle" TargetType="{x:Type UserControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type UserControl}">
                        <Border BorderBrush="{Binding Path=DP1}">
                            ...
                            <ContentPresenter ... Content="{TemplateBinding Content}"/>
                            ...
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
</UserControl>



其中, DP1 是类型的依赖属性。如果你直接设置其属性(如 DP1 ),它已通过这种方式创建的用户控件工作。这绝对不是真实的方式@Mario告诉我:

Where DP1 is a dependency property of type Brush. The UserControl which has been created through this way works if you set its properties (like DP1) directly. Absolutely this is not the true way as @Mario told me:

...当你使用一个用户控件,这意味着你的已经的了解它的布局,并且没有必要进一步样式的控制。要定义它的风格两倍的同时的因此导致碰撞...

...When you use an UserControl, it means that you already know its layout, and there is no need to style the control further. You are defining its style twice at the same time thus results a collision...

和他补充说

相反,你应该使用CustomControl;定义在主题的默认样式的文件夹(如果你拥有普通的Visual Studio,它会自动)。之后,你可以的覆盖的在自己的应用程序的默认样式。

Instead, you should use a CustomControl; Define the default style in the Themes folder (if you own regular Visual Studio, it makes automatically). Afterward, you may override the default style in your own app. In the same way you would do for a normal base class and its derived.

请按照此做.COM / KB / WPF / WPFCustomControl.aspx相对=nofollow> http://www.codeproject.com/KB/WPF/WPFCustomControl.aspx ...

Follow this: http://www.codeproject.com/KB/WPF/WPFCustomControl.aspx ...

显然,在这种情况下,我们需要得到我们从 ContentControl中类(而不是无外观控控制类)。你可以看看的这个&放大器; 的掌握细节。

Obviously, in this case we need to derive our lookless control from ContentControl class (instead of Control class). You may take a look at this & this to master the details.

在这里,我再次称谢@Mario。 ;)

Here, I give thanks to @Mario again. ;)

这篇关于问题与WPF用户控件应用样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆