c# - XAML-Datagrid 跳转到行

标签 c# list xaml datagrid

我在 C# 和 XAML 编程方面还很陌生,所以我会尽力尽可能准确地描述问题。

问题:

  • 我必须列出 EEPROM 中使用的地址和值。
  • 在 EEPROM 中,并不是所有的地址都被使用,所以它例如在列表中出现地址范围 0...0x0F、0x20...0x2F 和 0x71...0x72。
  • 用户可以为EEPROM删除或添加新项(意味着在列表中删除或添加新条目)
  • 插入新项目时,它会自动获取第一个可用地址。 (例如,如果使用 0x0 到 0x0F,则新项目获得地址 0x1F)
  • 列表必须按地址顺序显示。

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

问题:

为了显示列表,我在 XAML 中使用了 Datagrid 和 Binding。 该列表可能很长,因此要查看最后的元素,我必须向下滚动。 当我向下滚动并插入一个新项目时,该元素将插入到列表的第一个元素中,因此我必须向上滚动并找出插入的位置(例如编辑它)。

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

从昨天开始,我在谷歌上搜索了很多,尝试了一些我发现的东西,但我没有找到解决方案。

我的 XAML 文件的片段:

<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>

类 RightsConfigViewModel 的片段如下所示:

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() 之类的:

  1. 使用数据网格(C# 类从 XAML 获取信息)。
  2. 移动或滚动到项目(C# 类控件 GUI)。

在此先感谢您的帮助。

最良好的祝愿。

Sprite

最佳答案

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

关于c# - XAML-Datagrid 跳转到行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43801996/

相关文章:

c# - 用于检索可以为空的属性的 Lambda 表达式

python - 连接对象列表中的字符串

python - 如何在列表理解中转换这个 for 循环?

c# - 使用从外部类继承嵌套类

c# - Blazor - 集合的双向绑定(bind)

c# - WPF在不同窗口中镜像网格内容

按钮悬停时 XAML Windows 8 应用程序蓝色边框

c# - WPF 将集合的集合绑定(bind)到 DataGrid

c# - 使用静态事件或通过单例订阅的事件订阅?

python - 将元组列表成对组合成特定长度的新元组列表