2

내 사용자 정의 컨트롤의 스타일을 설정하려고합니다. 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> 
+0

할 수있는 가장 좋은 앱 리소스에 리소스 사전에로드하는 것입니다. 이렇게하면 응용 프로그램을 시작할 때 사용할 수 있습니다. – jjrdk

+0

프로젝트는 UserControlLibrary이므로이 작업을 수행 할 App.xaml 파일이 없습니다. –

답변

6

내가 볼 수있는 한 제대로 작동해야합니다. 특수 경고 또는 오류가 발생하거나 스타일이 적용되지 않습니다. 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

+0

감사합니다. 대단히 감사합니다. 예제를보고 작동 시켰습니다. :) –

1

새로운 리소스 사전을 만들고 거기에 스타일을 정의하고 앱 리소스에 추가 할 수 있습니다.

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

그리고 내 경험에

관련 문제