首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ContentPresenter中按钮前景的绑定路径填充

问ContentPresenter中按钮前景的绑定路径填充
EN

Stack Overflow用户
提问于 2013-11-01 03:55:26
回答 2查看 10.5K关注 0票数 5

我有一个按钮样式,带有一个包含ContentPresenter的模板,在该模板中,我试图将路径的填充绑定到按钮的前台:

代码语言:javascript
复制
<!-- This is inside the template of a button style -->
<ContentPresenter>
 <ContentPresenter.Resources>
  <Style TargetType="{x:Type Path}">
   <Setter Property="Fill" Value="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType=Button}}"/>
   </Style>
  </ContentPresenter.Resources>
</ContentPresenter>

我还有一个没有填充集的路径,我可以在按钮中引用它作为内容,如下所示:

代码语言:javascript
复制
<Button Style="{DynamicResource MyButtonStyle}" Content="{DynamicResource PathIcon}" Foreground="Blue"/>

我希望按钮内的路径是蓝色的,但它不是.它不能从按钮上抓住前景。

如何获得绑定到按钮颜色的路径?

谢谢!

P.S.:

如果我在值(即Value="Red")中添加了硬编码的颜色,则按钮内的路径为红色.所以我知道这有效..。

代码语言:javascript
复制
<ContentPresenter>
 <ContentPresenter.Resources>
  <Style TargetType="{x:Type Path}">
   <Setter Property="Fill" Value="Red"/>
   </Style>
  </ContentPresenter.Resources>
</ContentPresenter>

编辑:

下面是完整的样式和ControlTemplate:

代码语言:javascript
复制
<Style x:Key="Button_Style" TargetType="{x:Type Button}">
        <Setter Property="Foreground" Value="{StaticResource White_Brush}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid x:Name="grid" Background="Transparent">
                        <ContentPresenter>
                            <ContentPresenter.Resources>
                                <Style TargetType="{x:Type Path}">
                                    <Setter Property="Fill" Value="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType=Button}}"/>
                                </Style>
                            </ContentPresenter.Resources>
                        </ContentPresenter>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                               <!-- Should affect Text as well as Paths in the Content property of the button! -->                              
                               <Setter Property="Foreground" Value="{StaticResource Black_Brush}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-01 07:57:16

好吧,我们点:

它不能从按钮上抓住前景。

这种结构的风格:

代码语言:javascript
复制
RelativeSource={RelativeSource AncestorType=Button}

不会工作,因为Style只是集合的设置者,他不知道控制,是否有,特别是关于视觉树的内容。因为RelativeSource应该引用可视化树中的上面的项。为此目的,通常使用DataTemplate或ControlTemplate。

如果我在值中加一个硬编码的颜色(即Value=“红色”)

是的,在这种情况下,它将发挥作用,并且总是更好地创建表单的设计:

代码语言:javascript
复制
<SolidColorBrush x:Key="MyButtonColor" Color="Blue" />

并将其用于控制,如Button

代码语言:javascript
复制
<Button Background="{StaticResource MyButtonColor}" ... />

在Style或其他地方:

代码语言:javascript
复制
<Setter Property="Fill" Value="{StaticResource MyButtonColor}" />   

也就是说,最好不要依赖于元素参数(背景颜色等)。位于可视树中,因为它可以:

  • 可以移动到另一个面板(Grid,StackPanel)或UserControl
  • 可能会离开项目

和刷子在as资源将永远在一个地方,改变他们在这个地方,所有的元素,他们的拾取。此外,颜色可以存储在不依赖于特定技术实现(资源、变量)的特殊数据模型中,在这些实现中,数据可以来自外部源,例如项目/配置设置。

如果可能的话,最好避免由于不必要地使用系统性能(在某些情况下是内存泄漏)而使用动态资源,在您的情况下它们是不需要的。

动态资源通常是为SolidColorBrush和其他种类的刷子明确定义的,因为默认情况下它们是冻结的,而且由于上述原因(内存泄漏),它们不建议更改。更多信息可以在这里找到:

可冻结对象-- MSDN综述

Edit

据我所知,您希望为Button制作通用的Path或Text内容(在同时使用的情况下将更容易)。正如我前面已经提到的,RelativeSource应该在ControlTemplate周围,因此,Path将在Grid中与ContentPresenter一起使用。

要想知道到Tag (可选属性)的样式,这是为文本或路径提供的,它指示两个属性:OnlyText或OnlyPath。

为了设置Path的数据,我创建了一个附加的依赖项属性,并在ControlTemplate中指定了它。

以下是一个完整的例子:

XAML

代码语言:javascript
复制
<Window x:Class="ButtonPathHelp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ButtonPathHelp"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    WindowStartupLocation="CenterScreen"
    Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <SolidColorBrush x:Key="Green_Brush" Color="Green" />
        <SolidColorBrush x:Key="Black_Brush" Color="Black" />

        <Style x:Key="Button_Style" TargetType="{x:Type Button}">
            <Setter Property="Foreground" Value="{StaticResource Green_Brush}" />
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="VerticalAlignment" Value="Center" />

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid x:Name="grid">
                            <ContentPresenter x:Name="MyContent"
                                              Content="{TemplateBinding Content}"
                                              HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                                              VerticalAlignment="{TemplateBinding VerticalAlignment}" />

                            <Path x:Name="MyPath" 
                                  SnapsToDevicePixels="True" 
                                  Width="20" 
                                  Height="18" 
                                  Stretch="Fill" 
                                  Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}" 
                                  Data="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:MyDependencyClass.DataForPath)}" />
                        </Grid>

                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Foreground" Value="{StaticResource Black_Brush}"/>
                            </Trigger>

                            <Trigger Property="Tag" Value="OnlyText">
                                <Setter TargetName="MyPath" Property="Visibility" Value="Collapsed" />
                                <Setter TargetName="MyContent" Property="Visibility" Value="Visible" />
                            </Trigger>

                            <Trigger Property="Tag" Value="OnlyPath">
                                <Setter TargetName="MyPath" Property="Visibility" Value="Visible" />
                                <Setter TargetName="MyContent" Property="Visibility" Value="Collapsed" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <WrapPanel>
        <WrapPanel.Resources>
            <sys:String x:Key="Save">
                F1 M 20.5833,20.5833L 55.4167,20.5833L 55.4167,55.4167L 45.9167,55.4167L 45.9167,44.3333L 30.0833,44.3333L 30.0833,
                55.4167L 20.5833,55.4167L 20.5833,20.5833 Z M 33.25,55.4167L 33.25,50.6667L 39.5833,50.6667L 39.5833,55.4167L 33.25,
                55.4167 Z M 26.9167,23.75L 26.9167,33.25L 49.0833,33.25L 49.0833,23.75L 26.9167,23.75 Z
            </sys:String>

            <sys:String x:Key="Search">
                F1 M 23.4454,49.2637L 31.7739,41.1598C 30.6986,39.2983 30.4792,37.1377 30.4792,34.8333C 30.4792,27.8377 35.7544,
                22.1667 42.75,22.1667C 49.7456,22.1667 55.4167,27.8377 55.4167,34.8333C 55.4167,41.8289 49.7456,47.1042 42.75,
                47.1042C 40.5639,47.1042 38.5072,46.9462 36.7125,45.9713L 28.3196,54.1379C 27.0829,55.3746 24.6821,55.3746 23.4454,
                54.1379C 22.2088,52.9013 22.2088,50.5004 23.4454,49.2637 Z M 42.75,26.9167C 38.3777,26.9167 34.8333,30.4611 34.8333,
                34.8333C 34.8333,39.2056 38.3777,42.75 42.75,42.75C 47.1222,42.75 50.6667,39.2056 50.6667,34.8333C 50.6667,
                30.4611 47.1222,26.9167 42.75,26.9167 Z
            </sys:String>
        </WrapPanel.Resources>

        <Button Name="SaveButton"
                Style="{StaticResource Button_Style}" 
                Tag="OnlyPath"
                local:MyDependencyClass.DataForPath="{StaticResource Save}"
                Margin="10" />

        <Button Name="JustText"
                Style="{StaticResource Button_Style}" 
                Tag="OnlyText"
                Content="Just Text"
                Margin="10" />

        <Button Name="SearchButton"
                Style="{StaticResource Button_Style}" 
                Tag="OnlyPath"
                local:MyDependencyClass.DataForPath="{StaticResource Search}"
                Margin="10" />
    </WrapPanel>
</Window>

Code behind

代码语言:javascript
复制
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

public class MyDependencyClass : DependencyObject
{
    #region IsCheckedOnDataProperty

    public static readonly DependencyProperty DataForPathProperty;

    public static void SetDataForPath(DependencyObject DepObject, string value)
    {
        DepObject.SetValue(DataForPathProperty, value);
    }

    public static string GetDataForPath(DependencyObject DepObject)
    {
        return (string)DepObject.GetValue(DataForPathProperty);
    }

    #endregion

    static MyDependencyClass()
    {
        PropertyMetadata MyPropertyMetadata = new PropertyMetadata(String.Empty);

        DataForPathProperty = DependencyProperty.RegisterAttached("DataForPath",
                                                            typeof(string),
                                                            typeof(MyDependencyClass),
                                                            MyPropertyMetadata);
    }
}

Note:在Style中我没有使用TemplateBinding作为附加属性,因为TemplateBinding不能在模板之外或VisualTree属性之外工作,所以您甚至不能在模板的触发器中使用TemplateBinding。因此,我们必须使用构造{RelativeSource TemplatedParent}和一个等于要检索其值的依赖项属性的路径。

Output

​

​

要下载整个示例,请遵循这个链接。

票数 7
EN

Stack Overflow用户

发布于 2016-10-30 16:51:01

我无意中发现了一个简单的问题,但我想知道如何在按钮的禁用状态下找到它的“前景颜色”(以获得正确的画图颜色)。这是最后一个简单的解决办法。没有模板,没有样式,没有代码,什么都没有。正确的相对约束:-):

代码语言:javascript
复制
     <StackPanel Orientation="Horizontal">
        <Button Height="22" IsEnabled="False">
            <Polygon Points="4,0 4,5 5,5 2.5,10 0,5 1,5 1,0 " 
                     Fill="{Binding (TextElement.Foreground), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}}">
                <Polygon.LayoutTransform>
                    <RotateTransform Angle="90"></RotateTransform>
                </Polygon.LayoutTransform>
            </Polygon>
        </Button>
        <Button Height="22" IsEnabled="True">
            <Polygon Points="4,0 4,5 5,5 2.5,10 0,5 1,5 1,0 " 
                     Fill="{Binding (TextElement.Foreground), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}}">
                <Polygon.LayoutTransform>
                    <RotateTransform Angle="180"></RotateTransform>
                </Polygon.LayoutTransform>
            </Polygon>
        </Button>
    </StackPanel>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19720532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档