在WPF重写模板化按钮的命令 [英] Overriding a templated Button's Command in WPF
问题描述
我有一个控件模板
我使用改变几个按钮
S上的外观和行为。我只希望的按钮为可点击并执行绑定
命令
的一部分,所以我增加了一个按钮
在控件模板
,并将其绑定到模板的命令
属性。
I have a ControlTemplate
that I'm using to change the appearance and behavior of several Button
s. I only want part of the Button
to be clickable and to execute the bound Command
, so I added a Button
within the ControlTemplate
and bound it to the template's Command
property.
我遇到的问题是,因为我定义了命令
的模板按钮结合
,它不管我点击模板的哪一部分的执行。
The problem I'm having is that since I am defining the Command
binding on the templated Button
, it executes regardless of which part of the template I am clicking.
在下面的例子中,你可以点击边框
执行命令
。我如何改变它,使命令
只执行,当你在模板中点击按钮
?
In the below example you can click the Border
and execute the Command
. How do I change it so that the Command
only executes when you click the Button
within the template?
<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}" />
我不认为我能模板不同的元素(如边框
),因为我还需要在命令传递
不知何故,和附加属性仍然给我相同的行为。
I don't think I can template a different element (like the Border
) because I still need to pass in the Command
somehow, and attached properties would still give me the same behavior.
推荐答案
另一种方法是重新模板的另一个控件,如边界并在模板内的按钮使用的RoutedCommand。你可以绑定到内置RoutedCommands,如ApplicationCommands.Save和在XAML为含有控制之一,添加的CommandBinding该特定命令。让我知道,如果一些code将是有益的。
Another alternative is to re-template another control, such as Border and use a RoutedCommand on the Button inside the template. You could bind it to one of the built-in RoutedCommands, such as ApplicationCommands.Save and in the XAML for the containing control, add a CommandBinding for that particular command. Let me know if some code would be helpful.
这篇关于在WPF重写模板化按钮的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!