【问题标题】:How to execute a method of the control when a binding property is changed?更改绑定属性时如何执行控件的方法?
【发布时间】:2012-08-09 19:16:38
【问题描述】:

我有一个数据网格,我正在绑定 selectedItem 属性。我也知道 DataGrid 控件有一个 ScrollIntoView 方法,可以将 dataGrid 滚动到所选项目。

所以我想知道,当我在 viewModel 中更改 selectedItem 时,是否存在执行 dataGrid 的方法 ScrollIntoView 的方法。

我的 Datagrid 设置为多项选择,如果这很重要,我想使用 MVVM 模式。

谢谢。

【问题讨论】:

    标签: wpf mvvm datagrid scroll


    【解决方案1】:

    在后面的代码中定义一个dependency property 并将网格中的选择绑定到它。然后,您可以在值更改时添加回调。你可能想要这样的东西:

        public static readonly DependencyProperty SelectedItemsProperty =
            DependencyProperty.Register(
                "SelectedItems",
                typeof(object[]),
                typeof(Control),
                new PropertyMetadata(null, SelectedItemsChanged));
    

    定义回调函数并添加任何适当的逻辑。

    【讨论】:

    • 我忘了说我想使用 MVVM 模式。谢谢。
    • 你仍然可以。我认为依赖属性是 MVVM 模式的重要组成部分。能详细点吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 2021-01-31
    • 2012-11-11
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    相关资源
    最近更新 更多