Kap*_*íko 10 wpf xaml wpf-controls
我想设置Button的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>
Run Code Online (Sandbox Code Playgroud)
我决定将这些资源分开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>
Run Code Online (Sandbox Code Playgroud)
当我按下时,现在Path的Fill属性没有更新Button.有人可以解释为什么它不能单独工作ResourceDictionaries并为我提供这个问题的解决方案吗?
UPDATE
所以我发现问题就在于Path.当我有Path在<UserControl.Resources>,在颜色Colors.xaml和按钮样式的Controls.xaml一切再次工作.
我还发现,当我Path进入时Icons.xaml,我将其UserControl用作按钮的内容
<Button Command="{Binding Path=SmallPageSizeCommand}" Style="{StaticResource TransparentButtonStyle2}" Content="{StaticResource PageSmallIcon2}"/>
Run Code Online (Sandbox Code Playgroud)
我有多个我的实例UserControl...然后Path只出现在一个实例中UserControl.
Nov*_*i S 14
的原因:
我有多个UserControl实例...然后Path一次只出现在UserControl的一个实例中
是因为你在XAML中给予不同的父元素.您正在将多个Button控件的内容设置Path Shape为在资源中定义的内容.出于优化目的,对资源的每个引用将默认返回对象的相同实例,并且因为在WPF中每个元素一次只能有一个父元素,所以只有ButtonXAML声明中的最后一个元素才能将其内容设置为所需的内容Path.要解决此问题,您可以使用x:Shared ="False"属性标记资源.在这种情况下,XAML Parser将为每个Reference生成Path元素的新实例.