单击列表视图项内的按钮,在列表视图中选择项 [英] Select Item in list view on clicking a button inside a list view item

查看:90
本文介绍了单击列表视图项内的按钮,在列表视图中选择项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,每个列表视图项都包含一个Label和一个Button。单击按钮时,应选择相应的ListView项。



我创建了一个数据模板,以保留List视图项的Label和Button。 />


如何实现这一点,我尝试使用ICollectionView。但是我无法实现所需的功能。



I have a ListView, where each list view item will contain a Label and a Button. On clicking on the button, the respective ListView Item should be selected.

I have created a Data Template to keep the Label and Button for the List view items.

How can I achieve this, I tried by using ICollectionView. But I was not able to achieve the required functionality.

<Window.Resources>
<DataTemplate x:Key="dt2">
            <StackPanel Orientation="Horizontal">
                <Label Content="{Binding Path=FirstName}"/>
                <Button Content="Show" Command="{Binding RelativeSource = {RelativeSource    FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.ShowCustomer}"/>
            </StackPanel>
        </DataTemplate>
</Window.Resources>

<ListView ItemsSource="{Binding Path=Customers}" ItemTemplate="{StaticResource ResourceKey=dt2}" SelectedItem="{Binding CurrentCustomerToBeShown}" IsSynchronizedWithCurrentItem="True"/>

推荐答案

试试这个..





Try this..


<ListView ItemsSource="{Binding Path=List}" ItemTemplate="{StaticResource ResourceKey=dt2}" SelectedItem="{Binding CurrentCustomerToBeShown}" IsSynchronizedWithCurrentItem="True">
            <ListView.Resources>
                <Style TargetType="ListViewItem">
                    <Style.Triggers>
                        <Trigger Property="IsKeyboardFocusWithin" Value="True">
                            <Setter Property="IsSelected" Value="True"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.Resources>
        </ListView>


HI Mahesh感谢您的解决方案,每件事情都运转良好,但它不适用于第一项列表显示。第一个没有被点击按钮选中。



如何为TabControl应用相同的内容?



HI Mahesh thanks for the solution, every thing is working fine but it not working for the first item in the ListView. The first is not getting selected on clicking button.

How can I apply the same for a TabControl ?

<TabControl ItemsSource="{Binding CurrentlyShownCustomers}" SelectedItem="{Binding CurrentCustomer}" Grid.Column="1">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <ListView ItemsSource="{Binding CurrentlyShownCustomers}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding FirstName}"/>
                            <Button Height="20" Width="20" Content="X" Grid.Column="1" Command="{Binding RelativeSource={RelativeSource FindAncestor, 
                                AncestorType={x:Type TabControl}}, Path=DataContext.CloseTabItem}"/>
                        </StackPanel>
                    </ListView>
                </DataTemplate>
</TabControl>


你好



如果你不介意的话一些代码隐藏,这也完成了工作:

Hello

If you don't mind do some code-behind, this does the job too:
lb.PreviewMouseLeftButtonDown += (MouseButtonEventHandler)(
    (o, e) => {
        ListViewItem lbItem = UITools.FindAncestor<listviewitem>(e.OriginalSource as DependencyObject) as ListViewItem;
        bool isListViewItem = lbItem != null;
        bool isButtonClicked = UITools.FindAncestor<button>(e.OriginalSource as DependencyObject) is Button;
        if (isListViewItem )
        {
            if (isButtonClicked)
                lbItem.SetValue(ListBoxItem.IsSelectedProperty, true);
            else e.Handled = true; //Handled means will not pass to ListView for selection.
        }                    
    });



FindAncenstor方法: [ ^ ]


FindAncenstor method: from UITools.cs[^]

public static T FindAncestor<t>(DependencyObject obj) where T : DependencyObject
{
    while (obj != null)
    {
        T o = obj as T;
        if (o != null)
            return o;
        obj = VisualTreeHelper.GetParent(obj);
    }
    return default(T);
}</t>





问候

Joseph Leung



Regards
Joseph Leung


这篇关于单击列表视图项内的按钮,在列表视图中选择项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆