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 library
でclass library
からx:Key="YourKey"
でマークされたあなたのResources
を使用することができます
私にとってこれはうまく動作し、私もそれを使用します。
どのようにしていますか?あなたは適切なパックのURIを使用していますか? – AnjumSKhan
@AnjumSKhan Pack URI? – Belfed
リソース辞書の参照方法は? – AnjumSKhan