自动滚动到 datagridview MVVM light 中新添加的行

发布于 2024-12-04 04:49:41 字数 2141 浏览 0 评论 0原文

* 已解决 * 我终于在后面的代码中解决了它的“脏”问题,本来可以更干净&不过 MVVM 更好 可观察集合的内容仍然在 ViewModel 中完成...

    public ViewModelLocator VML = new ViewModel.ViewModelLocator();

    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {

        VML.MainMatch.Add();
            dataGrid.SelectedIndex = VML.MainMatch.Matches.Count - 1;
           dataGrid.ScrollIntoView(VML.MainMatch.Matches, dataGrid.Columns[0]);
    }

我有一个带有数据网格的视图和一个连接到中继命令的按钮,用于将记录添加到可观察集合(数据网格的项目源),以下工作正常:

View:

<Button x:Name="btnAdd" Content="{Binding Resource.btnAdd, Source={StaticResource CustomLocStrings}}" Width="100" Command="{Binding AddCommand, Mode=OneWay}" Margin="5" HorizontalAlignment="Center"  IsEnabled="{Binding IsLimitedUser, Converter={StaticResource TrueToFalseConverter}}"/>

ViewModel:

    AddCommand = new RelayCommand(() => Add());

    private void Add()
    {
       game _game = new game();
       _game.recid = 1;
       _game.teamid = GlobalVariables.CurrentUser.teamid;

       if (this.games != null)
       {
           this.games.Add(new gameViewModel(_game));
       }
       else
       {
            var _games = new List<gameViewModel>();
            _games.Add(new gameViewModel(_game));
            this.games = new ObservableCollection<gameViewModel>(_gamees);
       }

The问题是该行被添加到数据网格的底部、用户视图之外。 他必须向下滚动才能看到新记录。 我想要的是网格自动滚动到新记录的视图中。

我在这里发现了类似的问题(http://forums.lhotka.net/forums/p/9086/43212.aspx):但未能将其转化为我的情况:

我用 EventTocCommand 尝试过

     <i:Interaction.Triggers>
     <i:EventTrigger EventName="Click">
      <cmd:EventToCommand Command="{Binding Path=AddCommand}" PassEventArgsToCommand="True" CommandParameter="{Binding ElementName=dataGrid}"/>
                    </i:EventTrigger>

然后

    RelayCommand<EventArgs> AddCommand = new RelayCommand<EventArgs>(Add);

但没有任何反应我单击,我怀疑我的命令参数错误。

我希望有人能找到解决方案,因为我的用户界面并不是真正的“用户友好”。

提前致谢,

迈克

* SOLVED *
I finally solved it 'Dirty' in the code behind, could've been cleaner & nicer MVVM though
the observable collection stuff is still done in the ViewModel...

    public ViewModelLocator VML = new ViewModel.ViewModelLocator();

    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {

        VML.MainMatch.Add();
            dataGrid.SelectedIndex = VML.MainMatch.Matches.Count - 1;
           dataGrid.ScrollIntoView(VML.MainMatch.Matches, dataGrid.Columns[0]);
    }

I have a view with a datagrid and a button hooked up to a relaycommand to add records to the observable collection (datagrid's itemsource), the following works fine:

View:

<Button x:Name="btnAdd" Content="{Binding Resource.btnAdd, Source={StaticResource CustomLocStrings}}" Width="100" Command="{Binding AddCommand, Mode=OneWay}" Margin="5" HorizontalAlignment="Center"  IsEnabled="{Binding IsLimitedUser, Converter={StaticResource TrueToFalseConverter}}"/>

ViewModel:

    AddCommand = new RelayCommand(() => Add());

    private void Add()
    {
       game _game = new game();
       _game.recid = 1;
       _game.teamid = GlobalVariables.CurrentUser.teamid;

       if (this.games != null)
       {
           this.games.Add(new gameViewModel(_game));
       }
       else
       {
            var _games = new List<gameViewModel>();
            _games.Add(new gameViewModel(_game));
            this.games = new ObservableCollection<gameViewModel>(_gamees);
       }

The problem is that the row is added at the bottom of the datagrid, outside the users view.
He has to scroll down to see the new record.
What I want is that the grid automatically does a scrollintoview to the new record.

I've found a similar issue here (http://forums.lhotka.net/forums/p/9086/43212.aspx): but failed to translate it to my situation:

I tried it with EventTocCommand

     <i:Interaction.Triggers>
     <i:EventTrigger EventName="Click">
      <cmd:EventToCommand Command="{Binding Path=AddCommand}" PassEventArgsToCommand="True" CommandParameter="{Binding ElementName=dataGrid}"/>
                    </i:EventTrigger>

And then

    RelayCommand<EventArgs> AddCommand = new RelayCommand<EventArgs>(Add);

but nothing happens when I click, I suspect my commandparameter's wrong..

I hope someone has a solution since my user interface is not really 'user friendly' the way it is..

Thanks in advance,

Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぇ气 2024-12-11 04:49:41

我还没有尝试过,但我认为设置 SelectedItem 值得一试。

SelectedItem="{Binding SelectedItem}"

因此,创建对象,将其添加到网格中,并将 SelectedItem 设置为同一(新)对象。

I havent tried it, but i think setting the SelectedItem is going to be worth a try.

SelectedItem="{Binding SelectedItem}"

So, create your object, add it to the grid, and set the SelectedItem to the same (new) object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文