2016-12-03 13 views
0

本当に「高度なボタン」であるUserControlを作成しました。 私は依存関係のプロパティを実装しました。そのうちの1つはICommandです。これは、実際のウィンドウでコントロールが使用されているときにさらにバインド可能とされています。WPF C#userControlにバインドされたコマンドは起動しません。

ただし、何らかの理由でコマンドが機能しません。

通常のボタンで全く同じアプローチを試したところ、すべて正常に機能しました(つまり、私のDelegateCommand実装やViewModelの障害ではありません)。

私はバインドされたコマンドがなぜ起動しないのかをフォローアップしようとしましたが、信頼できない理由が見つかりませんでした。ここで

は私のUserControl 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

私はついにこの問題を解決しました。それはかなり簡単でした。

私はUserControlのボタンコマンドをUserControlのDependencyプロパティにバインドするのをやめました。このようにそれを変更する

はそれが仕事作っ:

<Button HorizontalContentAlignment="Stretch" Foreground="{Binding ElementName=ImageButtonUC, Path=Foreground}" 
     Background="{Binding ElementName=ImageButtonUC, Path=Background}" 
     Command="{Binding ElementName=ImageButtonUC, Path=ClickCommand}"> 
関連する問題