2016-12-03 53 views
0

所以,我創建了一個真正是「高級按鈕」的UserControl。 我已經實現了依賴屬性,其中之一是ICommand,當在實際窗口中使用控件時,應該進一步綁定它。WPF C#綁定到userControl的命令不會觸發

但是,由於某些原因命令不起作用。

當我在普通按鈕上嘗試完全相同的方法時,一切正常(因此它不是我的DelegateCommand實現或ViewModel的錯誤)。

我試圖跟進爲什麼綁定的命令不會觸發,但我找不到一個可靠的理由。

這裏是我的用戶XAML:

<Window x:Class="NoContact.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:NoContact" 
    mc:Ignorable="d" 
    xmlns:userControls="clr-namespace:NoContact.UserControls" 
    Title="MainWindow" Height="800" Width="960" Background="#22282a"> 
<Border BorderThickness="1" BorderBrush="#ffcd22" Margin="10,10,10,10"> 
<Grid> 
    <Button HorizontalContentAlignment="Stretch" Foreground="{Binding ElementName=ImageButtonUC, Path=Foreground}" 
      Background="{Binding ElementName=ImageButtonUC, Path=Background}"> 
     <DockPanel Width="{Binding ElementName=ImageButtonUC, Path=ActualWidth}"> 
      <Image Source="{Binding ElementName=ImageButtonUC, Path=Image}" DockPanel.Dock="Left" 
        Height="{Binding ElementName=ImageButtonUC, Path=ActualHeight, Converter={StaticResource HeightConverter}}" 
        Width="{Binding ElementName=ImageButtonUC, Path=ActualWidth, Converter={StaticResource HeightConverter}}" VerticalAlignment="Center"/> 
      <TextBlock Text="{Binding ElementName=ImageButtonUC, Path=Text}" FontSize="17" VerticalAlignment="Center" /> 
     </DockPanel> 
    </Button> 
</Grid> 
<UserControl.Resources> 
    <Style TargetType="{x:Type Button}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <Border Background="{TemplateBinding Background}"> 
         <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</UserControl.Resources> 

這裏是我的用戶後臺代碼:

public partial class ImageButton : UserControl 
{ 

    // OTHER IRRELEVANT CLASS PARAMETERS ARE HERE 

    public ICommand ClickCommand 
    { 
     get { return (ICommand)GetValue(ClickCommandProperty); } 
     set { SetValue(ClickCommandProperty, value); } 
    } 

    // OTHER IRRELEVANT DEPENDENCY PROPERTIES ARE HERE 

    public static DependencyProperty ClickCommandProperty = 
     DependencyProperty.Register("ClickCommand", typeof(ICommand), typeof(ImageButton)); 

    public ImageButton() 
    { 
     InitializeComponent(); 
    } 
} 

最後,這是我如何使用我的控制:

<userControls:ImageButton x:Name="phoneButton" ClickCommand="{Binding Path=MyButtonClickCommand}" Style="{StaticResource phoneImageButtonUCStyle}" Text="Telefon" Width="200" Height="100" VerticalAlignment="Top" /> 

該控件的DataContext設置在一個堆棧面板上,它圍繞着我的控件。我也嘗試了直接在控制上設置它,沒有效果。

再次,在常規按鈕上做同樣的工作很好。

+0

似乎有一些關閉標籤在你的XAML例子失蹤。嘗試添加這些內容,並讓示例更「複製粘貼」,這樣人們可以複製粘貼問題的當前狀態。從那裏開始找出解決方案更容易:) – Teknikaali

+0

'這裏是我的UserControl XAML: Will

回答

0

我終於解決了這個問題 - 這是相當平凡的。

我已經ommited最重要的綁定 - 用戶控件的按鈕命令UserControl的依賴項屬性。

這樣的改變也使得它的工作:

<Button HorizontalContentAlignment="Stretch" Foreground="{Binding ElementName=ImageButtonUC, Path=Foreground}" 
     Background="{Binding ElementName=ImageButtonUC, Path=Background}" 
     Command="{Binding ElementName=ImageButtonUC, Path=ClickCommand}">