gpt4 book ai didi

c# - 检索datagrid中的可见项列表

转载 作者:行者123 更新时间:2023-12-03 10:35:08 25 4
gpt4 key购买 nike

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

用户还应大声确认所有可见项目。这就是我卡住的地方。 是否可以从数据网格检索显示的当前集合列表?

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方法”是什么?

最佳答案

为了观察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;
}
}

DataGrid的定义的xaml中,您必须编写以下内容:
nameSpaceOfAttachedProperty:DataGridExtensions.ObserveVisiblePersons="True"
nameSpaceOfAttachedProperty:DataGridExtensions.VisiblePersons="{Binding VisiblePersons, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

在您的ViewModel中,您将拥有一个类似以下的属性:
private List<Person> visiblePersons;
public List<Person> VisiblePersons
{
get { return visiblePersons; }
set
{
visiblePersons = value;
OnPropertyChanged();
}
}

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

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

关于c# - 检索datagrid中的可见项列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34427456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com