我想请求帮助解决这个问题。 我的目标是为数据网格准备导航控制,就像在WinForms中一样。 向上移动,向下移动一行,向上移动一行,向下移动,并可能跳到行(按数字)。
我做了一些解决方案xaml:
<GroupBox x:Name="gbInfo"
BorderBrush="Black"
BorderThickness="1"
Margin="4"
Header="Info">
<StackPanel Orientation="Horizontal"
VerticalAlignment="Center">
<Button Content="<<"
Command="{Binding CmdMoveUpDown}"
CommandParameter="Down"/>
<TextBlock Text="{Binding InfoTable}"
VerticalAlignment="Center"/>
<Button Content=">>"
Command="{Binding CmdMoveUpDown}"
CommandParameter="Up"/>
</StackPanel>
</GroupBox>
<DataGrid x:Name="dgrForPrijem"
Margin="5"
ItemsSource="{Binding TabulkaPrijmu}"
AutoGenerateColumns="False"
AlternationCount="2"
SelectionMode="Single
CanUserAddRows="False"
SelectedItem="{Binding MySelectedItem}"
IsReadOnly="True" >
<intr:Interaction.Behaviors>
<atch:ScrollIntoViewBahaviour/>
</intr:Interaction.Behaviors>
<DataGrid.Columns>
<DataGridTextColumn x:Name ="dgtcForDpr"
Header="ID Car"
Width="80*"
Binding="{Binding DPR}" />
<DataGridTextColumn x:Name="dgtcForIdDpr"
Header="Type of Car"
Width="40*"
Binding="{Binding ID_DPR}" />
<DataGridTextColumn x:Name="dgtcForIdStav"
Header="Status"
Width="60*"
Binding="{Binding ID_STAV}"/>
</DataGrid.Columns>
</DataGrid>
这是视图模型:
public class VMIndexMaterialIncome : ViewModelBase
{
public VMIndexMaterialIncome()
{
TabulkaPrijmu = new ObservableCollection<TMaterialIncome>(new MaterialSupplyServices().ProvideAllMaterialIncomes());
}
private TMaterialIncome mySelectedItem;
public TMaterialIncome MySelectedItem
{
get
{
return mySelectedItem;
}
set
{
mySelectedItem = value;
InfoTable = TabulkaPrijmu.IndexOf(mySelectedItem).ToString();
NotifyPropertyChanged("MySelectedItem");
}
}
private string infoTable;
public string InfoTable
{
get { return infoTable; }
set
{
infoTable = value;
NotifyPropertyChanged("InfoTable");
}
}
public ICommand CmdMoveUpDown
{
get
{
return new RelayCommand<object>((object obj) =>
{
if (TabulkaPrijmu != null)
{
int myIndex = TabulkaPrijmu.IndexOf(MySelectedItem);
switch(obj.ToString())
{
case "Up":
if (myIndex < (TabulkaPrijmu.Count - 1))
myIndex++;
break;
case "Down":
if (myIndex > 0)
myIndex--;
break;
default:
break;
}
MySelectedItem = TabulkaPrijmu[myIndex];
}
});
}
}
private ObservableCollection<TMaterialIncome> tabulkaPrijmu;
public ObservableCollection<TMaterialIncome> TabulkaPrijmu
{
get
{
return tabulkaPrijmu;
}
set
{
tabulkaPrijmu = value;
NotifyPropertyChanged("TabulkaPrijmu");
}
}
}
}
这里有scrollBehaviour代码:
public class ScrollIntoViewBahaviour : Behavior<DataGrid>
{
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.SelectionChanged += new SelectionChangedEventHandler(AssociatedObject_SelectionChanged);
}
void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is DataGrid)
{
DataGrid grid = (sender as DataGrid);
if (grid.SelectedItem != null)
{
Action action = delegate () {
grid.UpdateLayout();
grid.ScrollIntoView(grid.SelectedItem, null);
grid.Focus(); // added this to make it focus to the grid
};
grid.Dispatcher.BeginInvoke(action);
}
}
}
protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.SelectionChanged -=
new SelectionChangedEventHandler(AssociatedObject_SelectionChanged);
}
}
一切正常 - 当我点击按钮 - &gt;选定的行向上或向下移动。 但是当我通过单击列标题对datagrid进行排序时。功能是错误的。 如果ObservableCollection具有不同的顺序(排序)并且datagrid具有不同的顺序。
是否有任何选项在点击标题(根据所选列)后对ObservableCollection进行排序? 当然,没有代码可以做到这一点?由于MVVM模式?
感谢您的帮助。