如何将按钮关联到ListView.SelectedItem [英] How to associate button to ListView.SelectedItem

查看:84
本文介绍了如何将按钮关联到ListView.SelectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到ObservableCollection的ListView ItemsSource.我通过MVVM添加了一个属性来跟踪ListView.SelectedItem.一个按钮已添加到我的ListView(通过GridViewColumn.CellTemplate),以创建一个按钮命令,该命令显示有关ObservableCollection中每个对象的数据.因此,我的ObservableCollection中的对象列表(ListView列1)在ListView中显示,并带有相应的按钮(ListView列2).

I have a ListView ItemsSource bound to an ObservableCollection. I added a property via MVVM to track the ListView.SelectedItem. A button was added to my ListView (via GridViewColumn.CellTemplate) to create a button command which displays data regarding each object in my ObservableCollection. So the list of objects in my ObservableCollection (ListView column 1) is shown in the ListView with a corresponding button (ListView column 2).

该代码非常有效!唯一的问题:用户必须先单击ListView行,然后再单击列表中的相应按钮. (如果用户单击按钮而不先单击ListView行,则会在"SelectedFromQueue"属性上获得空引用异常.)

The Code works great! The only problem: the user must click the ListView row before clicking the corresponding button in the list. (I get a null reference exception on my "SelectedFromQueue" property if the user clicks the button without clicking the ListView row first.)

我想添加单击按钮时设置ListView.SelectedItem属性的代码.因此,如果用户单击按钮,则代码应在执行关联的MVVM命令之前更新ListView.SelectedItem属性绑定.

I would like to add code which sets the ListView.SelectedItem property when a button is clicked. So, if the user clicks a button, the code should update the ListView.SelectedItem property binding before the associated MVVM command is executed.

有人为此提供任何示例代码吗?感谢您的帮助.

Does someone have any example code for this? Thanks for your help.

我的XAML:

<UserControl xmlns:local="clr-namespace:MyApp"
             x:Class="MyApp.QueueObjectList"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Height="290" Width="320">

    <Grid Width="319">
        <GroupBox Header="Queue Class List" HorizontalAlignment="Left" Width="319" BorderBrush="Black" BorderThickness="2">
            <ListView ItemsSource="{Binding Path=QueueList}" Name="QueueListView">
                <ListView.SelectedItem>
                    <Binding Path="SelectedFromQueue" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                    </Binding>               
                </ListView.SelectedItem>
                <ListView.View>
                    <GridView>
                        <GridViewColumn Width="140" Header="Queue Name" DisplayMemberBinding="{Binding Name}" />
                        <GridViewColumn Width="179" Header="Property Information">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Button Content="Get Property Info" Command="{Binding Path=GetQueueObjProperties}"
                                        DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}" />                      
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
        </GroupBox>
    </Grid>
</UserControl>

我的MainWindowViewModel C#代码:

My MainWindowViewModel C# Code:

private ObservableCollection<Queue> _QueueList;
private Queue _selectedFromQueue;

public ObservableCollection<Queue> QueueList
{
    get { return _QueueList; }
    set
    {
        _QueueList = value;
        RaisePropertyChanged("QueueList");
    }
}

public Queue SelectedFromQueue
{
    get { return _selectedFromQueue; }
    set
    {
        _selectedFromQueue= value;
        RaisePropertyChanged("SelectedFromQueue");
    }
}

// Constructor
public MainWindowViewModel()
{
    QueueList = new ObservableCollection<Queue>();
    _selectedFromQueue= null;
}

public ICommand GetQueueObjProperties
{
    get { return new RelayCommand(GetQueueProperties, CanGetQueueProperties); }
}

private bool CanGetQueueProperties()
{
    if (_QueueList.Count > 0)
    {
        return true;
    }
    return false;
}

private void GetQueueProperties()
{
    if (CanGetQueueProperties())
    {
        ResponseMessage.Add("Queue name: " +SelectedFromQueue.Name);
    }
}


更新: 谢谢你!


Update: Thanks sll!

我在XAML代码中添加了以下内容:

I added the following to my XAML code:

            <ListView.Resources>
                <Style TargetType="{x:Type ListViewItem}">
                    <EventSetter Event="PreviewGotKeyboardFocus" Handler="SelectCurrentItem"/>
                </Style>
            </ListView.Resources>

我在代码后面添加了以下c#方法:

I added the following c# method to code behind:

        protected void SelectCurrentItem(Object sender, KeyboardFocusChangedEventArgs e)
        {
            ListViewItem item = (ListViewItem)sender;
            item.IsSelected = true;
        }

效果很好! 再次感谢您的参考!!

Works great! Thanks again for the reference sll!

推荐答案

我不确定GetQueueProperties的作用,但听起来您使用SelectedFromQueue 知道要在哪个项目上执行逻辑以及在其中进行选择GUI是次要的.

I'm not sure what GetQueueProperties does, but it sounds that you use SelectedFromQueue to know on what item to execute your logic and the selection in GUI is secondary.

如果是这种情况,请不要使用SelectedFromQueue,而应将其添加到Button:

If that's the case, don't use SelectedFromQueue but add this to the Button:

CommandParameter="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListViewItem}}"

这将为您提供与Queue对象相关的对象,该对象与所按下按钮的行相关,如SelectedFromQueue中的e.Parameter.

This will give you a Queue object associated with the row of the pressed button as e.Parameter in SelectedFromQueue.

如果要做需要为GUI设置选定的项目,请在SelectedFromQueue的开头添加:

If you do need to set selected item for the GUI, at the start of SelectedFromQueue add:

SelectedFromQueue = (e.Parameter is Queue) ? e.Parameter : null

这篇关于如何将按钮关联到ListView.SelectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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