获取由控件模板加载的listview元素

时间:2013-10-23 05:07:32

标签: wpf listview

<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组成,Stackpanel本身有一个文本块和一个按钮。现在当我点击按钮时,我怎么知道点击了哪个listviewitem以便我可以进行一些操作?

1 个答案:

答案 0 :(得分: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 hanlder,您可以致电

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的当前项