2011-08-10 44 views
1

在下面的WPF XAML代碼中,如果我處於模板化Button的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> 

如果我嘗試獲取Parent或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; 
    } 

在上面的代碼中,我猜測它被抓住,從App.xaml中,其中該自定義ListBoxWithAddRemove控制定義。如何遍歷實際窗體的XAML而不是[上面顯示的第一個代碼]?

<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

可以使用VisualTreeHelper走了樹,如果你有正確類型的對象停下來,例如

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; 
    //... 
} 

如果你只是想選擇可以(也應該)只是通過建立的綁定完成的項目,如

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元素。我一直得到一個ListBoxItem的風格的一個DataContext代替TASKITEM: <形式的TargetType = 「ListBoxItem的」> Shawn

+1

@Shawn:這種風格很好,但你不應該像你這樣將它添加到列表框中,而是將其設置爲它的'ItemContainerStyle':<! - Style here - >' –

0

假設

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

以您想要的方式工作,您應該能夠遍歷DataContext中用作列表框的ItemsSource中的項目,並檢查Selected p找出當前選中的一個。確定ListBox中選定項目的更爲典型的方法是使用listBox.SelectedItem,其中listBox是引用相關ListBox的變量。或者,您可以通過參數sender參數SelectTaskItemClick方法訪問它。您可能嘗試的另一種方法是遍歷視覺樹的輔助方法,如The Coding BlokeThe Code Project - LINQ to Visual Tree中所述。

+0

我認爲按鈕的要點是選擇被點擊的項目,所以它沒有事先被選中。 –

相關問題