2011-12-07 5 views
1

簡単な質問です。私はWPFアプリケーション(.NET 4.0)を持っています。多くのユーザーパネルを含むリストボックスがあります。これらのユーザーパネルにはそれぞれチェックボックスがあります。CheckBoxの相互作用時に親ListBoxItemを選択してください。

実行中は、チェックボックス自体を除くユーザーパネルの任意の部分をクリックすると、リストボックスがその行を選択します(この単純なケースでは背景が視覚的に変化します)。チェックボックスをオンにすると、行は選択されません。

要件: チェックボックスをオンにすると、行が選択されるはずです。

チェックボックスコントロール:

<UserControl x:Class="CheckboxClickExample.CheckboxControl" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="189" d:DesignWidth="221"> 
    <Grid> 
     <CheckBox Content="CheckBox" Height="16" HorizontalAlignment="Left" Margin="10,10,0,0" Name="checkBox1" VerticalAlignment="Top" /> 
    </Grid> 
</UserControl> 

メインウィンドウ:

<Window x:Class="CheckboxClickExample.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:checkboxsample="clr-namespace:CheckboxClickExample" 
     Title="MainWindow" Height="350" Width="525"> 
    <ListBox>   
     <checkboxsample:CheckboxControl/> 
     <checkboxsample:CheckboxControl/> 
     <checkboxsample:CheckboxControl/> 
     <checkboxsample:CheckboxControl/> 
    </ListBox> 
</Window> 

答えて

2

あなたが背後にあるあなたのUserControlコードでこれを扱うことができる:

private void ButtonBase_OnClick(object sender, RoutedEventArgs e) 
    { 
     var parent = sender as DependencyObject; 

     while (parent != null) 
     { 
      if (parent is Selector) 
       break; 

      parent = VisualTreeHelper.GetParent(parent); 
     } 

     if (parent != null) 
      ((Selector) parent).SelectedItem = this; 
    } 

そして、あなたのCheckBoxでハンドラを使用します。

<CheckBox Content="CheckBox" 
      Height="16" 
      Click="ButtonBase_OnClick" 
      HorizontalAlignment="Left" 
      Margin="10,10,0,0" 
      VerticalAlignment="Top" /> 

編集

あなたが背後にあるコードを使用しない場合、私はあなたが行うことができると思う最高の添付行動などの既存のソリューションをパッケージ化することです。これは、コードを一度だけ記述すればよいという利点があり、UserControlの一部ではなくても、どのボタンでもプロパティを設定できるという利点があります。例えば

public static class ButtonClickHelper 
{ 
    public static void SetEnableSelectionOnClick(ButtonBase button, bool value) 
    { 
     button.SetValue(EnableSelectionOnClickProperty, value); 
    } 

    public static bool GetEnableSelectionOnClick(ButtonBase button) 
    { 
     return (bool) button.GetValue(EnableSelectionOnClickProperty); 
    } 

    public static readonly DependencyProperty EnableSelectionOnClickProperty = 
     DependencyProperty.RegisterAttached("EnableSelectionOnClick", typeof (bool), typeof (ButtonClickHelper), 
              new FrameworkPropertyMetadata(OnEnableSelectionOnClickPropertyChanged)); 

    private static void OnEnableSelectionOnClickPropertyChanged(DependencyObject d, 
                   DependencyPropertyChangedEventArgs e) 
    { 
     if (!(d is ButtonBase)) 
      return; 

     var button = (ButtonBase) d; 
     if ((bool) e.NewValue) 
     { 
      button.Click += OnButtonClick; 
     } 
     else 
     { 
      button.Click -= OnButtonClick; 
     } 
    } 

    private static void OnButtonClick(object sender, RoutedEventArgs e) 
    { 
     var parent = sender as DependencyObject; 
     var ancestors = new List<DependencyObject>(); 

     while (parent != null) 
     { 
      if (parent is Selector) 
       break; 

      parent = VisualTreeHelper.GetParent(parent); 
      ancestors.Add(parent); 
     } 

     if (parent != null) 
     { 
      var selector = (Selector) parent; 
      var itemToSelect = ancestors.Where(i => selector.Items.Contains(i)).FirstOrDefault(); 

      if (itemToSelect != null) 
       ((Selector) parent).SelectedItem = itemToSelect; 
     } 
    } 
} 

次に、あなただけのEnableSelectionOnClick依存関係プロパティを設定することで、あなたのXAMLでこれを使用することができます。

<CheckBox Content="CheckBox" 
      Height="16" 
      l:ButtonClickHelper.EnableSelectionOnClick="True" 
      HorizontalAlignment="Left" 
      Margin="10,10,0,0" 
      VerticalAlignment="Top" /> 

は、この情報がお役に立てば幸い!

関連する問題