2011-08-10 9 views
1

以下のWPF XAMLコードでは、テンプレートボタンのSelectTaskItemClickイベントで、現在選択されているListBoxItem ItemSourceオブジェクトを取得するにはどうすればよいですか?アイテムテンプレートコントロールイベントからListBoxItemを取得する

<!-- ListBox ITEMS --> 
    <TaskDash:ListBoxWithAddRemove x:Name="listBoxItems" Grid.Row="1" Grid.Column="3" Grid.RowSpan="3" 
     ItemsSource="{Binding}"> 
     <!--ItemsSource="{Binding}" DisplayMemberPath="Description">--> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="IsSelected" Value="{Binding Path=Selected}"/> 
     </Style> 
     <TaskDash:ListBoxWithAddRemove.ItemTemplate> 
      <DataTemplate> 
       <DockPanel> 
        <Button DockPanel.Dock="Left" Click="SelectTaskItemClick">SELECT</Button> 
        <TextBox DockPanel.Dock="Left" Name="EditableDescription" Text="{Binding Description}" Height="25" Width="100" /> 
        <Button DockPanel.Dock="Left" Click="EditTaskItemClick">EDIT</Button> 
       </DockPanel> 
      </DataTemplate> 
     </TaskDash:ListBoxWithAddRemove.ItemTemplate> 
    </TaskDash:ListBoxWithAddRemove> 

私が親またはTemplateParentを取得しようとすると、ContentPresenterまたはStyleなどが表示されます。上記のコードで

private void SelectTaskItemClick(object sender, RoutedEventArgs e) 
    { 
     Button taskItemButton = (Button) e.OriginalSource; 
     ContentPresenter taskItem = (ContentPresenter) taskItemButton.TemplatedParent; 
     taskItem = (ContentPresenter)taskItemButton.TemplatedParent; 
     Style taskItem2 = taskItem.TemplatedParent; 
     taskItem2 = taskItem.TemplatedParent; 
     DependencyObject taskItem3 = taskItem2.Parent; 
     //DependencyObject taskItem3 = taskItem2.TemplatedParent; 
     //TaskItem taskItemObj = taskItem2; 
    } 

、私はそれがカスタムListBoxWithAddRemoveコントロールが定義されているApp.XAMLからのことをつかんされて推測しています。実際のフォームのXAMLを代わりにトラバースするにはどうすればいいですか?あなたは正しい型のオブジェクトを持っている場合は、例えば、木を歩くと、停止するようにVisualTreeHelperを使用することができます

<Style x:Key="{x:Type TaskDash:ListBoxWithAddRemove}" TargetType="{x:Type  TaskDash:ListBoxWithAddRemove}"> 
      <Setter Property="Margin" Value="3" /> 
      <Setter Property="SnapsToDevicePixels" Value="True"/> 
      <Setter Property="OverridesDefaultStyle" Value="True"/> 
      <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/> 
      <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
      <Setter Property="MinWidth" Value="120"/> 
      <Setter Property="MinHeight" Value="20"/> 
      <Setter Property="AllowDrop" Value="true"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type TaskDash:ListBoxWithAddRemove}"> 
         <Grid> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="25" /> 
           <RowDefinition Height="*" /> 
          </Grid.RowDefinitions> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="*" /> 
           <ColumnDefinition Width="*" /> 
          </Grid.ColumnDefinitions> 

          <Button Grid.Column="0" Grid.Row="0" 
            Click="DeleteControlClick">Delete</Button> 
          <Button Grid.Column="1" Grid.Row="0" 
            Click="AddControlClick">Add</Button> 
          <Border 
           Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" 
            Name="Border" 
            Background="{StaticResource WindowBackgroundBrush}" 
            BorderBrush="{StaticResource SolidBorderBrush}" 
            BorderThickness="1" 
            CornerRadius="2"> 
           <ScrollViewer 
            Margin="0" 
            Focusable="false"> 
            <StackPanel Margin="0" IsItemsHost="True" /> 
           </ScrollViewer> 
          </Border> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

答えて

2

private void SelectTaskItemClick(object sender, RoutedEventArgs e) 
{ 
    var b = sender as Button; 
    DependencyObject item = b; 
    while (item is ListBoxItem == false) 
    { 
     item = VisualTreeHelper.GetParent(item); 
    } 
    var lbi = (ListBoxItem)item; 
    //... 
} 

あなただけ(と)だけ例えば、設立の結合を介して行われるべきである可能項目を選択する場合は)その

<Style TargetType="ListBoxItem"> 
    <Setter Property="IsSelected" Value="{Binding Path=Selected}"/> 
</Style> 

を想定し

private void SelectTaskItemClick(object sender, RoutedEventArgs e) 
{ 
    // The DataContext should be an item of your class that should 
    // have a Selected property as you bind to it in a style. 
    var data = (sender as FrameworkElement).DataContext as MyClass; 
    data.Selected = true; 
} 
+0

ありがとう、これはほとんど私が必要としていたものです。私もやってみたいことは、Style要素を削除することでした。 <スタイルのTargetType = "ListBoxItem"> Shawn

+1

@Shawn:私はSTYLEのDataContextの代わりにたTaskItemとListBoxItemを取得保管しました:スタイルはそれ自身では問題ありませんが、それをListBoxに追加するのではなく、 'ItemContainerStyle'として設定してください:' <! - Style here - > ' –

0

あなたが意図したように動作します。リストボックスのItemsSourceとして使用されているDataContext内のアイテムをループし、Selected p現在選択されているものを見つけるために、 ListBoxから選択された項目を判別するより一般的な方法は、listBox.SelectedItemを使用することです。ここで、listBoxは問題のListBoxを参照する変数です。あるいは、SelectTaskItemClickメソッドのパラメータsenderからアクセスすることもできます。あなたが試みるかもしれない別の方法は、The Coding BlokeThe Code Project - LINQ to Visual Treeで説明されているような視覚的なツリーを横断するヘルパーメソッドです。

+0

ボタンのポイントは、クリックされたアイテムを選択することだと思うので、あらかじめ選択されていません。 –

関連する問題