我想将Button's设置Content为Path在Resources. 所以我在资源中创建UserControl、定义Path、两个Brushes和按钮Style......一切都很好。
<UserControl ...>
<UserControl.Resources>
<Path x:Key="PageSmallIcon2" Stretch="Fill" Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}, Path=Foreground}" Stroke="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}, Path=Foreground}" SnapsToDevicePixels="True" Data="F1 M 19,25L 27,25L 27,33L 19,33L 19,25 Z M 19,36L 27,36L 27,44L 19,44L 19,36 Z M 31,25L 57,25L 57,33L 31,33L 31,25 Z M 19,47L 27,47L 27,55L 19,55L 19,47 Z "/>
<SolidColorBrush x:Key="MainColorBrush2" Color="Orange"/>
<SolidColorBrush x:Key="WhiteColorBrush2" Color="WhiteSmoke"/>
<Style x:Key="TransparentButtonStyle2" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="{StaticResource WhiteColorBrush2}"/>
<Setter Property="Height" Value="25"/>
<Setter Property="Width" Value="25"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Rectangle Fill="Transparent"/>
<ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}" Margin="2" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Foreground" Value="{StaticResource MainColorBrush2}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
<Button Command="{Binding Path=SmallPageSizeCommand}" Style="{StaticResource TransparentButtonStyle2}" Content="{StaticResource PageSmallIcon2}"/>
...
</Grid>
</UserControl>
我决定将这些资源放在单独的ResourceDictionaries. 我计划有更多...Paths而且Brushes我希望按钮只有一种样式。基本上我正在这样做,因为我的每个按钮都有样式,并且每个Path特定按钮Button都是Button.ControlTemplate
所以我创建了三个ResourceDictionaries Colors.xaml,Icons.xaml然后Controls.xaml我更新了App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Theme/Colors.xaml"/>
<ResourceDictionary Source="Theme/Icons.xaml"/>
<ResourceDictionary Source="Theme/Controls.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
NowPath的Fill属性在我按下它时没有更新Button。任何人都可以解释为什么它不能单独工作ResourceDictionaries并为我提供解决这个问题的方法吗?
更新
所以我发现问题出在Path. 当我拥有它时,一切Path中的<UserControl.Resources>颜色Colors.xaml和按钮样式Controls.xaml都会再次起作用。
我还发现,当我Path进入Icons.xaml并在我的UserControlas 按钮内容中使用它时
<Button Command="{Binding Path=SmallPageSizeCommand}" Style="{StaticResource TransparentButtonStyle2}" Content="{StaticResource PageSmallIcon2}"/>
我有多个我的实例UserControl...然后Path一次只出现在一个实例中UserControl。