在 XAML 中将参数传递给资源控件

问题描述 投票:0回答:1

我在 ControlTemplate 中定义了一个按钮作为资源,以便能够多次使用它。

<Grid.Resources>
   <ControlTemplate x:Key="GenericButton">
       <Button Text="Click Here"
                IsEnabled="{Binding SelectedItemProperty,
                           Converter={StaticResource IsNotNullConverter}}" />
   </ControlTemplate>
</Grid.Resources>

我希望按钮更加动态一点,并接收 SelectedItemProperty 名称作为参数。

目前,我以这种方式显示资源按钮(只要 SelectedItemProperty 名称具体拼写如上面的代码所示,就可以正常工作):

<VerticalStackLayout Padding="20" Spacing="20" VerticalOptions="Fill">
   <ContentView ControlTemplate="{StaticResource GenericButton}" />
</VerticalStackLayout>

相反,我想做这样的事情:

<ContentView ControlTemplate="{StaticResource GenericButton, Parameter=SelectedItemProperty}" />

资源按钮看起来像这样:

<ControlTemplate x:Key="GenericButton">
   <Button Text="Click Here"
            IsEnabled="{Binding PassedParameter,
                       Converter={StaticResource IsNotNullConverter}}" />
</ControlTemplate>

在调用的控件之间传递参数的概念是什么?以及如何解决这个问题?

xaml maui
1个回答
0
投票

由于您使用的是控件模板,因此您应该使用 TemplateBinding 而不是常规 Binding

更多信息这里

 <Button Text="Click Here"
                IsEnabled="{TemplateBinding SelectedItemProperty,
                           Converter={StaticResource IsNotNullConverter}}" />
© www.soinside.com 2019 - 2024. All rights reserved.