2016-10-16 8 views
1

私は複数のプロジェクトとソリューションを持っている:WPF - ユーザーコントロールに別のアセンブリからスタイルを適用

  • アプリケーション
  • User Control Library
  • 私は適用したいClass Library

に含まれるClass Library~ User Controlに含まれるStyleが挙げられる。問題は、私がUser Control Libraryで作業しているので、私がメインアプリケーションと同じようにApp.xaml内のStyleを参照することができないことと、User ControlClass LibraryStylesを使用する方法がわからないことです。私はへのClass Libraryの参照を追加しましたが、私がをUser Controlに設定したとき、私はStyleが見つかりません。では、現在の方法は何ですか?

+0

どのようにしていますか?あなたは適切なパックのURIを使用していますか? – AnjumSKhan

+0

@AnjumSKhan Pack URI? – Belfed

+0

リソース辞書の参照方法は? – AnjumSKhan

答えて

0

MarkupExtensionを使用できます。両方のファイルをclass libraryに追加するだけです。 StyleRefExt.csとして

WpfRdExt/StyleRefExtension.cs

public abstract class StyleRefExtension : MarkupExtension 
{ 
    #region Fields 

    /// <summary> 
    /// Property for specific resource dictionary 
    /// </summary> 
    protected static ResourceDictionary RD; 

    #endregion 

    #region Properties 

    /// <summary> 
    /// Resource key which we want to extract 
    /// </summary> 
    public string ResourceKey { get; set; } 

    #endregion 

    #region Public Methods 

    /// <summary> 
    /// Overriding base function which will return key from RD 
    /// </summary> 
    /// <param name="serviceProvider">Not used</param> 
    /// <returns>Object from RD</returns> 
    public override object ProvideValue(IServiceProvider serviceProvider) 
    { 
     return ProvideValue(); 
    } 

    public object ProvideValue() 
    { 
     if (RD == null) 
      throw new Exception(
       @"You should define RD before usage. 
      Please make it in static constructor of extending class!"); 
     return RD[ResourceKey]; 
    } 

    #endregion 
} 

実装:

<TextBox Style="{YourNamespace:StyleRefExt ResourceKey=YourKey}" /> 

public class StyleRefExt : StyleRefExtension 
{ 
    #region Constructors 

    static StyleRefExt() 
    { 
     RD = new ResourceDictionary { 
       Source = new Uri("pack://application:,,,/YourNamespace;component/Styles/YourStyle1.xaml")}; 

     RD.MergedDictionaries.Add(new ResourceDictionary { 
             Source = new Uri(
              "pack://application:,,,/YourNamespace;component/Styles/YourStyle2.xaml")}); 
    } 

    #endregion 
} 

その後は、このようなあなたのuser control libraryclass libraryからx:Key="YourKey"でマークされたあなたのResourcesを使用することができます

私にとってこれはうまく動作し、私もそれを使用します。

関連する問題