简体   繁体   English

使用 {x: Bind} 绑定到 XAML 属性,即替换 {TemplateBinding XAMLProperty}

[英]Using {x: Bind} to bind to an XAML property, i.e. replacing {TemplateBinding XAMLProperty}

It is stated in MSDN that 在 MSDN中声明

Starting with Windows 10, version 1809, you can use the x:Bind markup extension anywhere you use TemplateBinding in a ControlTemplate.从 Windows 10 版本 1809 开始,您可以在 ControlTemplate 中使用 TemplateBinding 的任何地方使用 x:Bind 标记扩展。

However, when I try to replace TemplateBinding with {x:Bind} whilst defining the style of a custom control, as so,但是,当我尝试用{x:Bind}替换TemplateBinding同时定义自定义控件的样式时,

<Style TargetType="local:PomodoroTimer" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:PomodoroTimer">
                    <Grid Width="300" Height="300" Background="{x:Bind Background}">
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

I get the following error: Unable to resolve symbol 'Background' .我收到以下错误: Unable to resolve symbol 'Background' What am I missing?我错过了什么?

x:Bind needs code-behind. x:Bind需要代码隐藏。 (see here ) (见这里

So, thanks to MainWindow.xaml.cs , this works:所以,感谢MainWindow.xaml.cs ,这有效:

<Window
    x:Class="Bindings.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:Bindings"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid>
        <Grid.Resources>
            <Style
                x:Key="CustomButtonStyle"
                TargetType="Button">
                <Style.Setters>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="Button">
                                <Border
                                    BorderBrush="{x:Bind BorderBrush, Mode=OneWay}"
                                    BorderThickness="{x:Bind BorderThickness, Mode=OneWay}">
                                    <ContentControl Content="{x:Bind Content, Mode=OneWay}" />
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style.Setters>
            </Style>
        </Grid.Resources>
        <Button
            BorderBrush="SkyBlue"
            BorderThickness="1"
            Content="Custom Button"
            Style="{StaticResource CustomButtonStyle}" />
    </Grid>
</Window>

For custom (templated) controls, I'd go with:对于自定义(模板化)控件,我会使用 go:

Text="{TemplateBinding Text}"

or要么

Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

for TwoWay bindings.对于TwoWay绑定。

If you want to do x:Bind inside the ControlTemplate , this answer might help.如果您想在ControlTemplate中执行x:Bind ,此答案可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM