设置用户控件的样式 [英] Set Style for user control

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

问题描述

我正在尝试为我的用户控件设置样式.UserControl 在Controls"项目中,主题在MainProject"项目中

I am trying to set a style for my user control. The UserControl is in a project "Controls" and the theme is in a project "MainProject"

<UserControl x:Class="Controls.OutputPanel"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        mc:Ignorable="d" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        x:Name="OutputControl"> 
   <!-- Style="{DynamicResource UserControlStyle}"> - I cant set the style here because the Resource Dictionary hasn't been defined yet -->

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/MainProject;component/Themes/MyTheme.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <!-- Now that the Resource Dictionary has been defined I need to set the style -->      

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <TextBox x:Name="textbox" 
                   ScrollViewer.VerticalScrollBarVisibility="Visible"
                   Text="{Binding ElementName=OutputControl, Path=TextProperty}"
                   IsReadOnly="True"
                   Style="{DynamicResource OutputTextBoxStyle}"/>

    </Grid>

</UserControl>

推荐答案

就我所见,这应该工作正常.您是否收到任何特殊警告或错误,或者样式中的某些部分没有得到应用?

That should be working fine as far as I can see. Do you get any special warnings or errors or do some parts from the Style not get applied?

要在设置Resources后设置样式,可以使用以下语法

To set the Style after Resources has been set, you can use the following syntax

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/MainProject;component/Themes/MyTheme.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
<UserControl.Style>
    <DynamicResource ResourceKey="UserControlStyle"/>
</UserControl.Style>

如果您在此之后仍然遇到问题,您可以将其与我在此处上传的示例应用程序进行比较:http://www.mediafire.com/?q1v98huubzw02zb

If you're still having problems after this you can compare it to my sample app which I uploaded here: http://www.mediafire.com/?q1v98huubzw02zb

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

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