XAML-Datagrid jump to row



我在编程C#和XAML方面很新,所以我尽力描述问题尽可能好。

问题:

  • 我必须列出eeprom中使用的地址和值。
  • 在EEPROM中并非所有地址都使用,因此它例如在列表中出现地址范围0 ... 0x0f,0x20 ... 0x2f和0x71 ... 0x72。
  • 用户可以为EEPROM删除或添加新项目(含义删除或添加列表中的新条目(
  • 当插入新项目时,它会自动获得第一个可用地址。(例如,如果使用0x0至0x0f,新项目获取地址0x1f(
  • 必须按地址顺序显示列表。

现在,该问题已完成98%。当列表以正确的顺序显示时,我可以从列表中添加和删除项目。(感谢所有在其他主题中写答案的人。我经常遇到类似问题(

问题:

要显示列表,我在XAML中使用DataGrid和绑定。列表可能很长,因此要查看我必须向下滚动的最后一个元素。当我向下滚动并插入一个新项目时,该元素是在列表的第一个元素之一中插入的,因此我必须滚动并找出插入的位置(例如,对其进行编辑(。

我的问题:可以自动跳到新的插入行?

自昨天以来,我已经谷歌搜索了很多东西,我尝试了一些我发现的东西,但我找不到解决方案。

我的xaml-file的片段:

<UserControl x:Class="UI.RightsConfigView"
  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" 
  xmlns:cal="http://www.caliburnproject.org"
  mc:Ignorable="d" 
  d:DesignHeight="500" d:DesignWidth="800"
  xmlns:conv="clr-namespace:conv">
 ... Background setting ...
<Grid x:Name="myGrid">
<DataGrid x:Name="ParamGrid" Grid.Row="0" ItemsSource="{Binding Values}" SelectedValue="{Binding SelectedValue}"  cal:Message.Attach="[Event MouseDoubleClick] = [Action MouseDoubleClicked]"                              AutoGenerateColumns="False" SelectionMode="Single" >
<DataGrid.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="New Value" cal:Message.Attach="CreateNewValue">                            
                    </MenuItem>
                    <MenuItem Header="Delete Value" cal:Message.Attach="DeleteValue">
                    </MenuItem>
                </ContextMenu>
            </DataGrid.ContextMenu>
 ... Tamplate ...
</DataGrid>
</Grid>
</UserControl>

类的片段rightsconfigvigviewmodel看起来像:

public class RightsConfigViewModel : BaseTabModel, ICloseDetail
{
    public ObservableCollection<ParameterValueModel> Values { get; set; } 
    private void GenerateDisplayList()
    {
          foreach (EEPROMParameter theItem in parameters)
                {
                    ... some cast etc. ...
                    ParameterValueModel theParameterModel = new ParameterValueModel(theItem, this, this);
                    Values.Add(theParameterModel);
                }
           ...
           this.NotifyOfPropertyChange(() => this.Values);
    }
    public void CreateNewValue()
    {
           // Insert at the right position    
           foreach (ParameterValueModel model in Values)
                {
                   if (tempAddress != model.Parameter.Address)
                   {
                        foundFreeAddress = true;
                        modelToAddBefore = Values.IndexOf(model);
                        break;
                    }
                 }
                 ....
                 Values.Add(newModel);
                 Values.Move(Values.IndexOf(newModel), modelToAddBefore);
                 this.NotifyOfPropertyChange(() => this.Values);
    }
}

在代码范围内,我想在方法中插入createNewvalue((somithing的方法:

  1. 获取datagrid(C#-Class从XAML获取信息(。
  2. 移动或滚动到项目(C#-Class Controlls GUI(。

事先感谢您的任何帮助。

最好的祝福。

elvys

您可以调用DataGridScrollIntoView方法,并将新添加的项目作为参数传递。在您的情况下,您将为ViewModel中的集合添加新值,因此您需要通知UI,该项目添加到DataGrid并将其滚动到视图中。

我终于找到了一个很好的解决方案:

基本上,我使用了http://www.codeproject.com/tips/125583/scrollintoview-for-a-datagrid-when-when-when-when-using-using-mvvm中提出的解决方案,但进行了一些更改以使其起作用(对我来说(。

像它一样复制了C#-Class,我更改了

  • 名称空间(只是为了适合我的项目(和
  • 在grid.dispatcher.begininvoke的行中,我施放了匿名函数,因此该行看起来像:
   grid.dispatcher.begininvoke(((动作(委托(    ...

在我的ViewModel方法中创建了新的列表元素创建的元素由SelectedValue

指称
SelectedValue = newModel;
this.NotifyOfPropertyChange(() => this.SelectedValue);

都是所有人!

最新更新