2011-05-17 242 views
3

我創建了一個繼承Button類的自定義按鈕。我添加了一些依賴屬性,我需要讓用戶指定按鈕的外觀。WPF自定義按鈕的命令綁定不起作用

該按鈕看起來不錯,依賴屬性也按我的意願工作。但是,無論我如何嘗試,Command屬性都不起作用。

這裏是我的示例:

public partial class CustomButton : 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?! 
    } 
} 

我的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> 

我用它在我的其他窗口控制是這樣的:

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

命令始終沒有被執行,爲什麼?我錯過了什麼嗎? 請幫忙。謝謝。

+0

如果您使用正常的按鈕,它會工作嗎?檢查輸出窗口是否有綁定錯誤 – 2011-06-04 19:37:00

+0

是托馬斯,如果我使用普通按鈕,它會起作用。 – CooKies 2011-06-09 15:39:07

+0

請提供您通過樣式應用的完整控件模板。 – 2012-11-19 17:09:22

回答

0

猜測你的模板只是難以真正點擊按鈕。作爲實驗,請嘗試將根StackPanel的背景設置爲固定Red而不是Transparent,然後查看是否可以更容易點擊。

+0

嗨,我試過,它不能解決它。僅供參考,這個問題似乎與命令綁定有關。因爲我可以將鼠標懸停在上面,所以我可以點擊,並且所有樣式似乎都在工作。只是該命令似乎沒有從父模板傳遞。 – CooKies 2011-06-09 15:45:10