wpf 来自资源的路径作为按钮的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16671969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:50:19  来源:igfitidea点击:

Path from Resources as Content of Button

wpfxamlwpf-controls

提问by Kapitán Mlíko

I would like to set Button's Contentto Pathdefined in Resources. So I created UserControl, defined Path, two Brushesand button Stylein resources... and everything was ok.

我想将Button's设置ContentPathResources. 所以我在资源中创建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>

I decided to put those resources in separate ResourceDictionaries. I plan to have more Pathsand Brushes... Also I would like to have only one style for button. Basically I am doing this, because I had style for each of my buttons and Pathfor each specific Buttonwas part of that Button's ControlTemplate.

我决定将这些资源放在单独的ResourceDictionaries. 我计划有更多PathsBrushes......此外,我希望按钮只有一种样式。基本上,我这样做,因为我的风格我的每个按钮和Path每个具体Button是部分ButtonControlTemplate

So I created three ResourceDictionariesColors.xaml, Icons.xamland Controls.xamland then I updated App.xaml

所以,我创建了三个ResourceDictionariesColors.xamlIcons.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>

Now Path's Fillproperty is not updated when I press that Button. Could anybody please explain why it does not work in separate ResourceDictionariesand provide me with solution to this problem?

当我按下Now 时Path,它的Fill属性没有更新Button。有人可以解释为什么它不能单独工作ResourceDictionaries并为我提供解决这个问题的方法吗?

UPDATE

更新

So I discovered that problem is in Path. When I have that Pathin <UserControl.Resources>, colors in Colors.xamland button style in Controls.xamleverything works again.

所以我发现问题出在Path. 当我Path<UserControl.Resources>,颜色Colors.xaml和按钮样式中的Controls.xaml所有内容再次起作用时。

I also discovered that when I have my Pathin Icons.xamland I use it in my UserControlas button's content

我还发现,当我Path进入Icons.xaml并在UserControl作为按钮的内容中使用它时

<Button Command="{Binding Path=SmallPageSizeCommand}" Style="{StaticResource TransparentButtonStyle2}" Content="{StaticResource PageSmallIcon2}"/>

and I have multiple instances of my UserControl... then Pathis present only in one instance of a UserControlat a time.

并且我有多个实例UserControl... thenPath一次只出现在一个实例中UserControl

回答by Novitchi S

The reason for:

的原因:

I have multiple instances of my UserControl... then Path is present only in one instance of a UserControl at a time

我有多个 UserControl 实例...然后 Path 一次只出现在一个 UserControl 实例中

is because you are giving the same Element different parents in XAML. You are setting the content of multiple Buttoncontrols to a PathShapewhich is defined in resources. For optimization purposes each reference to a Resource will return by default the same Instance of the object and since in WPF each element can have only one parent at a time, only the last Buttonin your XAML declaration will have it's content set to the desired Path. To solve this you can mark the resource with the x:Shared="False" attribute. In this case the XAML Parser will generate a new instance of the Path element for each Reference.

是因为您在 XAML 中为相同的 Element 赋予了不同的父级。您正在将多个Button控件的内容设置PathShape为在资源中定义的一个。出于优化目的,对 Resource 的每个引用默认将返回对象的相同实例,并且由于在 WPF 中,每个元素一次只能有一个父元素,因此只有ButtonXAML 声明中的最后一个会将其内容设置为所需的Path. 要解决此问题,您可以使用 x:Shared="False" 属性标记资源。在这种情况下,XAML 解析器将为每个引用生成 Path 元素的新实例。