0
<ListView  x:Name="CustomWorkoutListView" ItemsSource="{Binding WorkoutTypeDTO}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Style="{DynamicResource ListViewStyle1}" Width="400" Height="119" SelectionChanged="CustomWorkoutSelectionChanged">
                            <ListView.ItemContainerStyle>
                                <Style TargetType="ListBoxItem">
                                    <Setter Property="Margin" Value="0"/>
                                    <Setter Property="Padding" Value="0"/>
                                    <Setter Property="BorderThickness" Value="0"/>
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                                <StackPanel>
                                                    <StackPanel Margin="0,0,10,8"  Name="WorkoutTypeBackground"  Visibility="{Binding IsEditable,Converter={StaticResource NegBooleanToVisibilityConverter}}">
                                                        <StackPanel.Background>
                                                            <ImageBrush ImageSource="/Assets/Images/workout_type_back_unselected.png"/>
                                                        </StackPanel.Background>
                                                        <TextBlock Name="TextBlockName" Text="{Binding WorkoutTypeName}" Style="{StaticResource WorkoutTypeNameText}"/>
                                                    </StackPanel>
                                                    <StackPanel Margin="0,0,10,8" Orientation="Horizontal" Visibility="{Binding IsEditable,Converter={StaticResource BooleanToVisibilityConverter}}">
                                                        <StackPanel.Background>
                                                            <ImageBrush ImageSource="/Assets/Images/workout_type_back_edit.png"/>
                                                        </StackPanel.Background>
                                                        <TextBlock Text="{Binding WorkoutTypeName}" Style="{StaticResource WorkoutTypeNameText}"/>
                                                        <Button BorderThickness="0" Template="{DynamicResource ButtonBaseControlTemplate1}" Name="CrossButton" Width="20" Height="20" Click="DeleteWorkoutType">
                                                            <Button.Background>
                                                                <ImageBrush ImageSource="/Assets/Images/workout_type_X.png"/>
                                                            </Button.Background>
                                                        </Button>
                                                    </StackPanel>
                                                </StackPanel>
                                                <ControlTemplate.Triggers>
                                                    <Trigger Property="IsSelected"  Value="true">
                                                        <Setter TargetName="WorkoutTypeBackground" Property="Background">
                                                            <Setter.Value>
                                                                <ImageBrush ImageSource="/Assets/Images/workout_type_back_selected.png"/>
                                                            </Setter.Value>
                                                        </Setter>
                                                        <Setter TargetName="TextBlockName" Property="FontSize" Value="48"/>
                                                    </Trigger>
                                                </ControlTemplate.Triggers>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </ListView.ItemContainerStyle>
                            <ListView.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <WrapPanel Orientation="Horizontal"/>
                                </ItemsPanelTemplate>
                            </ListView.ItemsPanel>
                        </ListView>

在上面的代码中,listviewitems 由 Stackpanel 组成,它本身有一个文本块和一个按钮。现在,当我单击按钮时,我怎么知道单击了哪个 listviewitem 以便我可以进行一些操作?

4

1 回答 1

0

如果您想在代码隐藏中处理此问题,则可以使用 VisualTreeHelper 来获取单击的 Button 的父级。您可以使用下面的方法到达 ti

    public T GetParent<T>(DependencyObject child)  where T : DependencyObject 
    {
        var parent = VisualTreeHelper.GetParent(child);
        if(parent != null)
        {
            if(parent is T)
            {
                return parent as T;
            }

            return GetParent<T>(parent);

        }
        else
        {
            return null;
        }
    }

并从 Button Click 处理程序中,您可以调用

ListViewItem item = GetParent<ListViewItem>(sender);

并将item.DataContext为您提供支持您的项目的数据。

但我建议您通过在 ViewModel 中定义 Command 并将当前项目作为其 CommandParameter 发送来处理这种 MVVM 方式

<Button Command="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" CommandParameter="{Binding}" BorderThickness="0" Template="{DynamicResource ButtonBaseControlTemplate1}" Name="CrossButton" Width="20" Height="20" Click="DeleteWorkoutType">

这将为您提供在命令处理程序中支持 ListViewItem 的当前项目

于 2013-10-23T05:25:38.937 回答