2017-03-07 1 views
1

私はUserControlを作成し、ResourcesDictionaryを書きました。いくつかのスタイルがあります。userControlのResourceDictionaryをapp.xamlにマージできますか?

そして、私はResourceDictionaryをApp.xamlにマージしたいと思います。

しかし、メインプロジェクトはAで、App.xamlがあります。

UserControlはBプロジェクトに入っているので、別の場所に住んでいます。

どのようにResourceDictonaryをマージできますか?

ここは私のコードの一部ですが、動作しません。 App.xamlで

まず試しは

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="myUserControl.xaml"/> 
     </ResourceDictionary.MergedDictionaries> ...... 

セカンド

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="pack://application:,,,/B;component/SubFolderName/myUserControl.xaml" /> 
     </ResourceDictionary.MergedDictionaries> ...... 

第三には、

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/B;component/SubFolderName/myUserControl.xaml" /> 
     </ResourceDictionary.MergedDictionaries> ...... 

彼らはすべてを試してみ動作しませんでした.....

+ myUserControl.xaml

<UserControl x:Class="........." 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:B" 
     DataContext="{Binding RelativeSource={RelativeSource Self}}" 
     mc:Ignorable="d" d:DesignWidth="300" d:DesignHeight="100"> 
<UserControl.Resources> 
    <ResourceDictionary> 

     <Style x:Key="TitleTest" TargetType="TextBlock" BasedOn="{StaticResource MetroTextBlock}"> 
      <Setter Property="Margin" Value="15"/> 
      <Setter Property="VerticalAlignment" Value="Top"/> 
      <Setter Property="HorizontalAlignment" Value="Left"/> 
      <Setter Property="FontSize" Value="20"/> 
     </Style> 

     <Style x:Key="PanelTestStyle" TargetType="StackPanel"> 
      <Setter Property="ClipToBounds" Value="True"/> 
      <Setter Property="Height" Value="55"/> 
      <Setter Property="VerticalAlignment" Value="Bottom"/> 
      <Setter Property="Margin" Value="15"/> 
     </Style> 
     <Style x:Key="TestStyle2" TargetType="TextBlock" BasedOn="{StaticResource MetroTextBlock}"> 
      <Setter Property="VerticalAlignment" Value="Bottom"/> 
      <Setter Property="HorizontalAlignment" Value="Right"/> 
      <Setter Property="FontSize" Value="40"/> 
     </Style>   
    </ResourceDictionary> 
</UserControl.Resources> 

<Grid> 
    <TextBlock Text="{Binding StatusTitle}" Style="{DynamicResource TitleTest}"/> 
    <StackPanel Style="{StaticResource PanelTestStyle}"> 
     <TextBlock x:Name="testText" Style="{DynamicResource TestStyle2}" Text="{Binding StatusNumber}"/> 
     <TextBlock x:Name="testText2" Style="{DynamicResource TestStyle2}" Text="TEST" /> 
    </StackPanel> 
</Grid> 

また、私はセッターの値がCSコード内のFontSizeを変更するには取得したいので、私がする必要がありますDynamicResource(TextBlocks)を設定します。

+0

myUserControl.xamlのxamlを投稿できますか? –

+2

コントロールを辞書にマージすることはできません.Xamlをユーザーコントロールからアプリケーションのリソースセクションに移動するか、スタンドアロンリソースディクショナリで作成します。 – MikeT

+0

@MikeTはい、そのためにxamlコードを確かに彼は本当にUserControlとマージしようとします。 –

答えて

0

を動作するはずです「第二試してみてください」; App.xamlではなくあなたがあなたの最初の試みのコードを使用してアクセスすることができます同じDLLにこののResourceDictionaryを使用したい場合は

<ResourceDictionary> 
    <Style x:Key="TitleTest" TargetType="TextBlock" BasedOn="{StaticResource MetroTextBlock}"> 
     <Setter Property="Margin" Value="15"/> 
     <Setter Property="VerticalAlignment" Value="Top"/> 
     <Setter Property="HorizontalAlignment" Value="Left"/> 
     <Setter Property="FontSize" Value="20"/> 
    </Style> 
... 

:このファイルが見えるように持っています。あなたが別のDLLでそれを使いたいなら、2回目のtryのコードを使ってアクセスすることはできません。

+0

私は理解しています!ありがとう。 – parfum

+0

幸運。もし私の答えを受け入れるなら、私はthankfullになります:) –

0

あなたは「A」、プロジェクト内の「B」をプロジェクトへの参照を持っている必要があり、その後、あなたの例では、あなたがsepereateのXAMLファイルであなたのResourceDictionaryを配置する必要が

関連する問題