wpf wpf样式设置器中的模板绑定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20799186/
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 10:18:51  来源:igfitidea点击:

TemplateBinding in wpf style setter?

c#wpf

提问by Karthi Keyan

I'm using <setter>in my wpf application and i need to use TemplateBinding for that setter Property to evaluate that value at compile time but i can't use TemplateBinding,its throwing an error,

<setter>在我的 wpf 应用程序中使用,我需要为那个 setter 属性使用 TemplateBinding 来在编译时评估该值,但我不能使用 TemplateBinding,它抛出一个错误,

My Code Is Below:

我的代码如下:

                <ControlTemplate TargetType="{x:Type srcview:ButtonView}">
                    <Button>
                        <Button.Style>
                            <Style TargetType="{x:Type Button}">
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="Background" Value="{TemplateBinding Color}"></Setter>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Button.Style>
                    </Button>
                </ControlTemplate>

How can i use TemplateBindingin my style setter or Is there any other way to evaluate the value at compile time?

我如何TemplateBinding在我的样式设置器中使用或者有没有其他方法可以在编译时评估值?

回答by gomi42

Indeed setters do not support TemplateBinding. Try this instead:

事实上,setter 不支持 TemplateBinding。试试这个:

<Setter Property="Background"
        Value="{Binding Color, RelativeSource={RelativeSource Self}}"/>

But be aware the color property you are refering to must be of type brush. Background is a brush and you cannot bind a color to a brush.

但请注意,您所指的颜色属性必须是笔刷类型。背景是画笔,您不能将颜色绑定到画笔。

回答by Mark Travis

If you are desparate to use TemplateBinding, then reverse the role your trigger is playing. Have it set it's values based on IsMouseOver being False, and then use TemplateBinding directly on the button. The downside with this approach is you will have to supply static values in the trigger. For example.

如果您急于使用 TemplateBinding,那么请反转您的触发器所扮演的角色。让它根据 IsMouseOver 为 False 来设置它的值,然后直接在按钮上使用 TemplateBinding。这种方法的缺点是您必须在触发器中提供静态值。例如。

<Window x:Class="StackOverflow._20799186.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ControlTemplate x:Key="ControlAsButtonTemplate" TargetType="{x:Type ContentControl}">
            <Button x:Name="MyButton" Content="Hello World!" Background="{TemplateBinding Background}" />

            <ControlTemplate.Triggers>
                <Trigger SourceName="MyButton" Property="IsMouseOver" Value="False">
                    <Setter TargetName="MyButton" Property="Background" Value="Silver" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Window.Resources>

    <ContentControl Template="{StaticResource ControlAsButtonTemplate}" Background="Green" />
</Window>

Note the user of the ControlTemplate.Triggers rather than the Button.Style.Triggers.

请注意 ControlTemplate.Triggers 而不是 Button.Style.Triggers 的用户。

I hope this helps.

我希望这有帮助。

回答by Radenko Zec

You probably need to have Color property defined in template in order to bind to that property.

您可能需要在模板中定义 Color 属性才能绑定到该属性。