Set Style for user control

蹲街弑〆低调 提交于 2019-12-04 12:11:15

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?

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>

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

You could make new resource dictionary, define your style there and than add it in app resources.

<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>

And

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!