我有一个ControlTemplate
,我用它来改变几个Button
的外观和行为。我只希望部分Button
可点击并执行绑定Command
,因此我在ControlTemplate
内添加了Button
,并将其绑定到模板的Command
属性。在WPF中重写模板按钮的命令
我遇到的问题是,由于我在模板化的Button
上定义了Command
绑定,因此无论我点击哪个模板部分,它都会执行。
在下面的示例中,您可以单击Border
并执行Command
。如何更改它,以便Command
仅在您单击模板内的Button
时执行?
<ControlTemplate x:Key="ButtonControlTemplate" TargetType="{x:Type Button}">
<Border BorderThickness="10" BorderBrush="Black">
<Button
Command="{TemplateBinding Command}"
CommandParameter="{TemplateBinding CommandParameter}" >
<ContentPresenter Content="{TemplateBinding Content}" />
</Button>
</Border>
</ControlTemplate>
...
<Button
Command="{Binding Path=SomeViewmodelCommand}"
CommandParameter="{Binding Path=SomeViewmodelCommandParameter}"
Content="Click"
Template="{StaticResource ButtonControlTemplate}" />
我不认为我能模板不同的元素(如Border
),因为我还需要在Command
某种方式通过,并附加属性仍然给我相同的行为。