Вы можете использовать следующие MarkupExtension
. Вам просто нужно добавить оба файла в ваш class library
.
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
}
Внедрение в StyleRefExt.cs
:
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
}
Затем вы можете использовать ваш Resources
отмечен x:Key="YourKey"
от class library
в вашем user control library
, как это:
<TextBox Style="{YourNamespace:StyleRefExt ResourceKey=YourKey}" />
Для меня это прекрасно работает, и я тоже использую его.
Как вы это делаете? Вы используете правильный пакет uri? – AnjumSKhan
@ UnjumSKhan Pack URI? – Belfed
Как найти ссылку на ресурсный словарь? – AnjumSKhan