检索datagrid中的可见项列表

时间:2015-12-23 02:02:32

标签: c# wpf mvvm datagrid observablecollection

我在基本数据网格中保留当前警报和事件的列表,用户可以滚动列表。用户可以选择一个项目并确认该特定警报。这有效。

用户还应大声承认所有可见项目。这就是我陷入困境的地方。 是否可以从数据网格中检索当前收集的列表?

XAML的一部分

<Grid Background="DarkGray">
    <StackPanel Orientation="Horizontal">
        <StackPanel Orientation="Vertical">
            <Button Content="Ack visible" Command="{Binding AcknowledgeVisibleCommand}"/>
            <Button Content="Ack selected" Command="{Binding AcknowledgeSelectedCommand}"/>
        </StackPanel>    
        <DataGrid ItemsSource="{Binding AlarmAndEventList}"  SelectedItem="{Binding SelectedAlarmItem}" Background="DarkGray" IsReadOnly="True">
        </DataGrid>
    </StackPanel>
</Grid>

ViewModel的一部分

public ObservableCollection<AlarmItem> AlarmAndEventList { get { return  _AlarmAndEventList; } }

    private void AcknowledgeVisible()
    {
        //Do something with a list of visible items in datagrid
    }

    private void AcknowledgeSelected()
    {
        if (SelectedAlarmItem != null)
        {
            AlarmAndEventInstance.updateAlarmItem(SelectedAlarmItem);
        }


    }

另外,如果用户双击某个项目,我想运行单个确认命令。什么是响应双击数据网格的“MVVM方式”?

2 个答案:

答案 0 :(得分:2)

要观察 if(isset($emailAddress)) { $obj=new JobOrders($site_id); $obj->load($record["id"]); $obj->sendTemplateEMail("My Joborder 2", $emailAddress); } 中当前可见的项目,我已编写以下附加属性:

DataGrid

public class DataGridExtensions { public static readonly DependencyProperty ObserveVisiblePersonsProperty = DependencyProperty.RegisterAttached( "ObserveVisiblePersons", typeof(bool), typeof(DataGridExtensions), new PropertyMetadata(false, OnObserveVisiblePersonsChanged)); public static readonly DependencyProperty VisiblePersonsProperty = DependencyProperty.RegisterAttached( "VisiblePersons", typeof(List<Person>), typeof(DataGridExtensions), new PropertyMetadata(null)); private static readonly DependencyProperty SenderDataGridProperty = DependencyProperty.RegisterAttached( "SenderDataGrid", typeof(DataGrid), typeof(DataGridExtensions), new PropertyMetadata(default(DataGrid))); private static void OnObserveVisiblePersonsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DataGrid dataGrid = d as DataGrid; if (dataGrid == null) return; dataGrid.Loaded += DataGridLoaded; } private static void DataGridLoaded(object sender, RoutedEventArgs routedEventArgs) { DataGrid dataGrid = (DataGrid) sender; dataGrid.Loaded -= DataGridLoaded; ScrollViewer scrollViewer = FindChildren<ScrollViewer>(dataGrid).FirstOrDefault(); if (scrollViewer != null) { SetSenderDataGrid(scrollViewer, dataGrid); scrollViewer.ScrollChanged += ScrollViewerOnScrollChanged; } } public static void SetObserveVisiblePersons(DependencyObject element, bool value) { element.SetValue(ObserveVisiblePersonsProperty, value); } public static bool GetObserveVisiblePersons(DependencyObject element) { return (bool) element.GetValue(ObserveVisiblePersonsProperty); } private static void SetSenderDataGrid(DependencyObject element, DataGrid value) { element.SetValue(SenderDataGridProperty, value); } private static DataGrid GetSenderDataGrid(DependencyObject element) { return (DataGrid) element.GetValue(SenderDataGridProperty); } private static void ScrollViewerOnScrollChanged(object sender, ScrollChangedEventArgs e) { ScrollViewer scrollViewer = sender as ScrollViewer; if (scrollViewer == null) return; ScrollBar verticalScrollBar = FindChildren<ScrollBar>(scrollViewer).FirstOrDefault(s => s.Orientation == Orientation.Vertical); if (verticalScrollBar != null) { DataGrid dataGrid = GetSenderDataGrid(scrollViewer); int totalCount = dataGrid.Items.Count; int firstVisible = (int) verticalScrollBar.Value; int lastVisible = (int) (firstVisible + totalCount - verticalScrollBar.Maximum); List<Person> visiblePersons = new List<Person>(); for (int i = firstVisible; i <= lastVisible; i++) { visiblePersons.Add((Person) dataGrid.Items[i]); } SetVisiblePersons(dataGrid, visiblePersons); } } public static void SetVisiblePersons(DependencyObject element, List<Person> value) { element.SetValue(VisiblePersonsProperty, value); } public static List<Person> GetVisiblePersons(DependencyObject element) { return (List<Person>) element.GetValue(VisiblePersonsProperty); } private static IList<T> FindChildren<T>(DependencyObject element) where T : FrameworkElement { List<T> retval = new List<T>(); for (int counter = 0; counter < VisualTreeHelper.GetChildrenCount(element); counter++) { FrameworkElement toadd = VisualTreeHelper.GetChild(element, counter) as FrameworkElement; if (toadd != null) { T correctlyTyped = toadd as T; if (correctlyTyped != null) { retval.Add(correctlyTyped); } else { retval.AddRange(FindChildren<T>(toadd)); } } } return retval; } } 定义的xaml中,你必须写下以下内容:

DataGrid

在您的ViewModel中,您将拥有如下属性:

nameSpaceOfAttachedProperty:DataGridExtensions.ObserveVisiblePersons="True"
nameSpaceOfAttachedProperty:DataGridExtensions.VisiblePersons="{Binding VisiblePersons, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

每次滚动时,VisiblePersons都会更新。

在您的情况下,您必须更改附属资产内的列表类型以符合您的要求。

答案 1 :(得分:-1)

  1. 您可以使用MyDataGrid.Items集合过滤掉。

  2. 您还可以在MyDataGrid中应用过滤。但请勿显示已过滤的商品。 How to filter DataGrid