ScrollIntoView为WPF的DataGrid(MVVM) [英] ScrollIntoView for WPF DataGrid (MVVM)

查看:1130
本文介绍了ScrollIntoView为WPF的DataGrid(MVVM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的MVVM模式,我创建了一个XAML为DataGrid的的SelectedItem绑定。我编程设置的SelectedItem,但是当我这样做的DataGrid不滚动选择。有没有什么办法可以做到这一点,而不完全打破MVVM模式?

I'm using the MVVM pattern, and I've created a binding in XAML for the SelectedItem of a DataGrid. I programatically set the SelectedItem, however when I do so the DataGrid does not scroll to the selection. Is there any way I can achieve this without completely breaking the MVVM pattern?

我发现下面的解决办法,但我得到一个错误,当我尝试实施行为类,即使我已经安装了混合SDK:的 http://www.codeproject.com/Tips/125583/ScrollIntoView-for-a-DataGrid-when-using-MVVM

I found the following solution but I get an error when I try to implement the Behavior class, even though I've installed Blend SDK: http://www.codeproject.com/Tips/125583/ScrollIntoView-for-a-DataGrid-when-using-MVVM

推荐答案

这应该工作。这个想法是你有这样的附加属性,你将附加到的DataGrid 。在附加了它的XAML,你将其绑定到一个属性上的视图模型。每当你想以编程方式分配一个值的SelectedItem ,您还可以设置一个值,这个属性,其附加属性绑定到

This should work. The idea is you have this attached property that you will attach to the DataGrid. In the xaml where you attach it, you'll bind it to a property on your ViewModel. Whenever you want to programmatically assign a value to the SelectedItem, you also set a value to this property, which the attached property is bound to.

我所做的附加属性的类型是无论的SelectedItem 类型,但说实话也无所谓的类型是什么,只要你把它设置为比它以前的不同。这种附加属性仅仅是被用来作为一种手段来执行的视图控件的一些代码(在这种情况下,的DataGrid )在MVVM友好的方式。

I've made the attached property type to be whatever the SelectedItem type is, but honestly it doesn't matter what the type is as long as you set it to something different than what it was before. This attached property is just being used as a means to execute some code on the view control (in this case, a DataGrid) in an MVVM friendly fashion.

所以,这说,这里是附加属性的代码:

So, that said, here's the code for the attached property:

namespace MyAttachedProperties
{
    public class SelectingItemAttachedProperty
    {
        public static readonly DependencyProperty SelectingItemProperty = DependencyProperty.RegisterAttached(
            "SelectingItem",
            typeof(MySelectionType),
            typeof(SelectingItemAttachedProperty),
            new PropertyMetadata(default(MySelectionType), OnSelectingItemChanged));

        public static MySelectionType GetSelectingItem(DependencyObject target)
        {
            return (MySelectionType)target.GetValue(SelectingItemProperty);
        }

        public static void SetSelectingItem(DependencyObject target, MySelectionType value)
        {
            target.SetValue(SelectingItemProperty, value);
        }

        static void OnSelectingItemChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var grid = sender as DataGrid;
            if (grid == null || grid.SelectedItem == null)
                return;

            // Works with .Net 4.5
            grid.Dispatcher.InvokeAsync(() => 
            {
                grid.UpdateLayout();
                grid.ScrollIntoView(grid.SelectedItem, null);
            });

            // Works with .Net 4.0
            grid.Dispatcher.BeginInvoke((Action)(() =>
            {
                grid.UpdateLayout();
                grid.ScrollIntoView(grid.SelectedItem, null);
            }));
        }
    }
}

和这里的XAML片段:

And here's the xaml snippet:

<Window ...
        xmlns:attachedProperties="clr-namespace:MyAttachedProperties">
    ...
        <DataGrid 
            attachedProperties:SelectingItemAttachedProperty.SelectingItem="{Binding MyViewModel.SelectingItem}">
            ...
        </DataGrid>
    </Grid>

这篇关于ScrollIntoView为WPF的DataGrid(MVVM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆