2012-01-16 3 views
0

ComboBoxのポップアップセグメントにボタンを持つComboBoxのコントロールテンプレートがあります。ControlTemplateのカスタムボタンでICommandイベントを使用する

ICommandインターフェイスを使用して[その他]ボタンのクリックに反応するにはどうすればよいですか?

  <Popup x:Name="PART_Popup" 
    IsOpen="{TemplateBinding IsDropDownOpen}"> 
       <Border x:Name="PopupBorder" 
    HorizontalAlignment="Stretch" Height="Auto" 
    MinWidth="{TemplateBinding ActualWidth}" 
    MaxHeight="{TemplateBinding MaxDropDownHeight}" 
    BorderThickness="{TemplateBinding BorderThickness}" 
    BorderBrush="Black" Background="White" CornerRadius="3"> 
        <ScrollViewer x:Name="ScrollViewer" BorderThickness="0" Padding="1"> 
         <StackPanel> 
         <ItemsPresenter/> 
         <Button>Other</Button> 
         </StackPanel> 
        </ScrollViewer> 

       </Border> 

      </Popup> 

答えて

2

H.B.の答えはうまくいくが、もっと邪魔にならない方法は、新しいattached propertyです。

名前をMoreCommandとしましょう。

コントロールテンプレートでMoreCommandにButtonをバインドし、ComboBoxインスタンスを宣言した時点でMoreCommandをViewModelのコマンドにバインドします。

詳細。

1)あなたはあなたがこの添付プロパティのためにあなたのコントロールテンプレートに

  <Popup x:Name="PART_Popup" 
    IsOpen="{TemplateBinding IsDropDownOpen}"> 
       <Border x:Name="PopupBorder" 
    HorizontalAlignment="Stretch" Height="Auto" 
    MinWidth="{TemplateBinding ActualWidth}" 
    MaxHeight="{TemplateBinding MaxDropDownHeight}" 
    BorderThickness="{TemplateBinding BorderThickness}" 
    BorderBrush="Black" Background="White" CornerRadius="3"> 
        <ScrollViewer x:Name="ScrollViewer" BorderThickness="0" Padding="1"> 
         <StackPanel> 
         <ItemsPresenter/> 
         <Button Command={TemplateBinding local:AttachedCommand.Command}>Other</Button> 
         </StackPanel> 
        </ScrollViewer> 
       </Border> 
      </Popup> 

をサポートを追加)添付プロパティに

public static class AttachedCommand 
{ 
    public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(AttachedCommand)); 

    public static bool GetCommand(DependencyObject dependencyObject) 
    { 
     return (bool)dependencyObject.GetValue(CommandProperty); 
    } 

    public static void SetCommand(DependencyObject dependencyObject, bool value) 
    { 
     dependencyObject.SetValue(CommandProperty, value); 
    } 
} 

2を提供して静的クラスを作成するには、ユーザーが定義していることを確認してください「ローカル」名前空間

3)あなたのビューでは、あなたはあなたに添付プロパティをバインドするには、利点は、あなたがコンボボックスのサブクラスを作成する必要がないということです、そしてあなたが潜在的に複数の「カスタマイズ」を組み合わせることができモデル

<ComboBox ItemsSource={Binding Items} local:AttachedCommand.Command={Binding FetchMoreItems} /> 

を見ますクラスの爆発のリスクなしに、将来同じコントロールを使用することができます。

+0

ComboBox.MoreCommandというコンボボックスにプロパティがあり、コマンドをオンにできます。これは、私のビューモデルのICommandになりますか? – Tjaart

+0

答えに実装の詳細を追加しました。 –

+0

私は時間の制約のために回避策をとらなければならなくなりましたが、これは間違いなく今後の私の選択方法になります。実装の詳細は間違いなく助けられました。なぜなら私はWPFの新機能だからです。ありがとう! – Tjaart

0

コンボボックスは、そのための任意の「部屋」を持っていない、あなたはコンボボックスから継承し、その後、Button.Commandに結合することができICommand財産をもたらす可能性があります。

関連する問題