Xaml 如何在UWP中使用x:bind-insideControlTemplate

Xaml 如何在UWP中使用x:bind-insideControlTemplate,xaml,uwp,uwp-xaml,Xaml,Uwp,Uwp Xaml,嗨,我只是想在UWP中的按钮控制模板中使用x:Bind我的简单代码如下 <Grid> <TextBox x:Name="txtWidth"/> <Button x:Name="btnEllipse" PointerEntered="btnEllipse_PointerEntered" PointerExited="btnEllipse_PointerExited"

嗨,我只是想在UWP中的按钮控制模板中使用x:Bind我的简单代码如下

<Grid>
<TextBox x:Name="txtWidth"/>       
    <Button x:Name="btnEllipse" PointerEntered="btnEllipse_PointerEntered" PointerExited="btnEllipse_PointerExited" Click="btnEllipse_Click">
        <Button.Template>
            <ControlTemplate>
                <Ellipse x:Name="myEll" Width="{x:Bind  ShapeWidth,Mode=OneWay}" Height="{Binding Width,ElementName=myEll}" Fill="Purple" Stroke="Black" StrokeThickness="2" />
            </ControlTemplate>
        </Button.Template>
    </Button>
</Grid>
我得到一个错误 在ControlTemplate中使用x:Bind需要使用“目标类型” 请让我知道我哪里出错了

另一个场景我们可以在此处使用Binding或椭圆宽度中的x:bind绑定txtWidth吗?

基于此,当您尝试在ControlTemplate中使用x:bind时,需要添加TargetType属性(例如
)。但是,x:bind的功能类似于TemplateBinding,它只能绑定Button的属性,因此如果您想绑定在code behind中声明的属性,最好使用绑定并声明DataContext。例如:

.xaml:

<Button x:Name="btnEllipse"  PointerExited="btnEllipse_PointerExited" Click="btnEllipse_Click">
    <Button.Template>
        <ControlTemplate>
            <Ellipse x:Name="myEll" Width="{Binding ShapeWidth}" Height="{Binding Width,ElementName=myEll}" Fill="Purple" Stroke="Black" StrokeThickness="2" />
        </ControlTemplate>
    </Button.Template>
</Button>
另一种情况是我们可以使用Binding或x:bind-in绑定txtWidth 椭圆宽度在这里

如果要将txtWidth的宽度与Ellipse的宽度绑定,可以使用ElementName查找txtWidth元素,并使用binding与其宽度绑定

<TextBox x:Name="txtWidth" Width="100" Text="efwiehfiweh"/>
<Button x:Name="btnEllipse"  PointerExited="btnEllipse_PointerExited" Click="btnEllipse_Click">
    <Button.Template>
        <ControlTemplate>
            <Ellipse x:Name="myEll" Width="{Binding Width,ElementName=txtWidth,Mode=OneWay}" Height="{Binding Width,ElementName=myEll}" Fill="Purple" Stroke="Black" StrokeThickness="2" />
        </ControlTemplate>
    </Button.Template>
</Button>


Hi,以下方法有帮助吗?你还有其他问题吗?嗨,Faywang两种方法对我都有效谢谢你的帮助,我已标记为已回答
this.DataContext = this;
<TextBox x:Name="txtWidth" Width="100" Text="efwiehfiweh"/>
<Button x:Name="btnEllipse"  PointerExited="btnEllipse_PointerExited" Click="btnEllipse_Click">
    <Button.Template>
        <ControlTemplate>
            <Ellipse x:Name="myEll" Width="{Binding Width,ElementName=txtWidth,Mode=OneWay}" Height="{Binding Width,ElementName=myEll}" Fill="Purple" Stroke="Black" StrokeThickness="2" />
        </ControlTemplate>
    </Button.Template>
</Button>