【发布时间】:2020-11-02 09:52:27
【问题描述】:
背景
我的解决方案中有多个项目,在一个项目中,我创建了一个包含合并 ResourceDictionary 的静态类,我在所有其他项目中都使用了它:
namespace GUI.Shared.Resources
{
public static class ResourceFactory
{
public static ResourceDictionary _resources = null;
public static ResourceDictionary Resources
{
get
{
if (_resources == null)
{
_resources = new ResourceDictionary();
_resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("pack://application:,,,/GUI.Shared;component/Resources/VectorIcons.xaml") });
_resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("pack://application:,,,/GUI.Shared;component/Resources/ButtonStyles.xaml") });
}
return _resources;
}
}
}
}
在 xaml 文件中定义我的样式ButtonStyles.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="SpecialButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Red"/>
</Style>
</ResourceDictionary>
我通常使用这样的样式:
<UserControl x:Class="GUI.Modules.TextControl.Views.TextControlView"
x:Name="TextControl"
xmlns:resources="clr-namespace:GUI.Shared.Resources;assembly=GUI.Shared">
<Grid>
<Button Style="{Binding Path=[SpecialButtonStyle], Source={x:Static resources:ResourceFactory.Resources}}">
</Grid>
</UserControl>
问题
现在我想在 View 中本地扩展我的样式。我发现您是通过使用 BasedOn 属性来执行此操作的。
<Button>
<Button.Style>
<Style TargetType="Button" BasedOn="{Binding Path=[SpecialButtonStyle], Source={x:Static resources:ResourceFactory.Resources}}">
</Style>
</Button.Style>
</Button>
只允许我的绑定方法,报错:
不能在
Style类型的BasedOn属性上设置Binding。Binding只能在 DependencyObject 的 DependencyProperty 上设置。
如何将ResourceFactory.Resources 中的这种样式用作BasedOn 样式?
【问题讨论】:
标签: wpf xaml resourcedictionary