从项模板控件事件中获取ListBoxItem

时间:2011-08-10 21:29:44

标签: c# wpf xaml styles

在下面的WPF XAML代码中,如果我在模板化Button的SelectTaskItemClick事件中,如何获取当前选中的ListBoxItem ItemSource对象?

    <!-- 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 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

假设

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

以您想要的方式工作,您应该能够遍历DataContext中用作列表框的ItemsSource的项目,并检查每个项目的Selected属性以查找当前选定的项目。从ListBox中确定所选项的更典型方法是使用listBox.SelectedItem,其中listBox是引用相关ListBox的变量。或者,您也可以通过sender方法将其访问SelectTaskItemClick方法。您可能尝试的另一种方法是遍历可视树的辅助方法,如The Coding BlokeThe Code Project - LINQ to Visual Tree中所述。