【问题标题】:Set Style for user control为用户控件设置样式
【发布时间】:2011-03-01 11:54:05
【问题描述】:

我正在尝试为我的用户控件设置样式。 UserControl 位于项目“Controls”中,主题位于项目“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>

【问题讨论】:

  • 根据我的经验,最好的办法是在应用程序资源中加载资源字典。这使它在应用程序启动时可用。
  • 该项目是一个 UserControlLibrary,所以没有 App.xaml 文件可以做到这一点

标签: wpf user-controls styles resourcedictionary dynamicresource


【解决方案1】:

据我所知,这应该可以正常工作。您是否收到任何特殊警告或错误,或者样式中的某些部分没有被应用?

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

<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

【讨论】:

  • 非常感谢,看看你的例子并让它工作:)
【解决方案2】:

您可以制作新的资源字典,在那里定义您的样式,然后将其添加到应用资源中。

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:UC="clr-namespace:UserControls;assembly=UserControls">
   <Grid>
      <UC:myUserControl/>
   </Grid>
</Window>


<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:UC="clr-namespace:UserControls;assembly=UserControls">

    <Style TargetType="UC:myUserControl">
       ...
    </Style>
</ResourceDictionary>

还有

【讨论】:

    猜你喜欢
    • 2016-07-05
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    相关资源
    最近更新 更多