开发者

WPF Custom Button's Command Binding doesn't work

开发者 https://www.devze.com 2023-03-06 03:37 出处:网络
I created a custom button that inherits the Button class. I added some dependency property that I need to let the user specify how the button will look like.

I created a custom button that inherits the Button class. I added some dependency property that I need to let the user specify how the button will look like.

The button looks great and the dependency property also worked as I wished. But however, the Command property doesn't work no matter how I try.

Here's my sample:

public partial class CustomButton开发者_StackOverflow社区 : Button
{
    ... //some dependency property
    public CustomButton()
    {
       ...//do some initialization
       //I tried checking the Command property over here but it seems like always to be NULL?!
    }
}

My XAML:

<Button x:Class="CustomButton"
        xmlns:.....>
   <Button.Resources>
   <Style TargetType="Button" x:Key="CustomButtonStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate  TargetType="{x:Type Button}">
                    <StackPanel Orientation="Horizontal" Background="Transparent" Height="22">
                    ......more things behind...
   </Style>
   </Button.Resources>
   <Button.Style>
   <Style TargetType="{x:Type Button}" BasedOn="{StaticResource CustomButtonStyle}">
   </Style>
   </Button.Style>
</Button>

I used it in my other windows control like this:

 <Controls:CustomButton Command="{Binding NewCommand}" ToolTip="AddNew"/>

The command always not being executed, why? Did I miss out anything? Please help. Thanks.


Guessing your template just makes it difficult to actually click on the button. As an experiment, try setting the background of the root StackPanel to solid Red rather than Transparent and see if that makes it easier to click.

0

精彩评论

暂无评论...
验证码 换一张
取 消