【发布时间】:2017-11-17 16:56:44
【问题描述】:
在我的 WPF 项目中,我有一个如下所示的数据网格:
<DataGrid x:Name="dgConfig" BorderThickness="5" AutoGenerateColumns="False" Margin="10,10,10,15" ItemsSource="{Binding ModulesView, Mode=TwoWay}" FontSize="14" Background="White">
<DataGrid.Columns>
<DataGridTextColumn Header="ParamName" Binding="{Binding ParamName}" />
<DataGridTextColumn Header="ParamValue" Binding="{Binding ParamValue}" />
<DataGridTextColumn Header="DefaultValue" Binding="{Binding DefaultValue}" />
<DataGridTextColumn Header="MaxValue" Binding="{Binding MaxValue}"/>
<DataGridTextColumn Header="MinValue" Binding="{Binding MinValue}"/>
<DataGridTextColumn Header="Address" Binding="{Binding Address}" SortDirection="Ascending"/>
</DataGrid.Columns>
</DataGrid>
现在我想获得ParamValue 的Row 值。在这种情况下,我需要将ParamValue按Address的顺序放入registerArray,那么我应该如何设置Datagrid按Address的顺序显示,然后将ParamValue放入数组 registerArray 按Address 的顺序排列?非常感谢!
--------------------------------更新-------------- ----------------------------------------
定义模块视图:
public ICollectionView ModulesView
{
get { return _ModulesView; }
set
{
_ModulesView = value;
NotifyPropertyChanged();
}
}
使用模块视图:
private void RefreshModule()
{
ModulesView = new ListCollectionView(sdb.GetModules())
{
Filter = obj =>
{
var Module = (Module)obj;
return SelectedProduct != null && SelectedProduct.ModelNumber == Module.ModelNumber;
}
};
}
【问题讨论】:
-
2 个问题,首先,您是如何通过代码隐藏或使用 ItemsSource“ModulesView”填充数据网格的?其次,您的排序应该已经正确设置,因为您在地址列上使用了
SortDirection="Ascending"。 -
首先我在 ViewModel 中使用
ICollectionView ModulesView来填充数据网格,其次我不知道为什么但是 Row 中的数据@ 987654336@ 没有默认排序,直到我单击行标题。 -
对于排序问题,在设置 ItemsSource 之前通过
OrderBy()对项目进行排序是否可以接受?这样您就不必依赖 Datagrid 排序。 DataGrid SortDirection ignored。你还有一个小错字Header="Addrsss"应该是Header="Address" -
至于您的
ParamValue问题,是否有类似用户可以点击的按钮,并且应该触发registerArray被填充?如果是这样,您可以在代码的任何位置使用registerArray = ModulesView.OrderBy(mod => mod.Address).Select(mod => mod.ParamValue).ToArray();来获取数组。请注意,您需要using System.Linq;。 -
@KeyurPATEL 抱歉,我的 ModulesView 似乎无法调用 OrderBy 方法。