【问题标题】:Force DataGrid into edit mode when using ListView for CellTemplate将 ListView 用于 CellTemplate 时强制 DataGrid 进入编辑模式
【发布时间】:2010-08-17 07:38:42
【问题描述】:

您好,

在 WPF DataGridTemplateColumn 中,我有一个使用 ListView 的 CellTemplate 和一个使用 DataGrid 的 CellEditingTemplate。

<DataTemplate x:Key="LimitsTemplate">
    <ListView ItemsSource="{Binding Limits}" IsEnabled="False">
        <ListView.ItemTemplate>
            ...
        </ListView.ItemTemplate>
    </ListView>
 </DataTemplate>
 <DataTemplate x:Key="LimitsEditingTemplate">
      <toolkit:DataGrid ItemsSource="{Binding Limits}" ...>
            ...
      </toolkit:DataGrid>
 </DataTemplate>

我面临的问题是如何在双击时强制列进入编辑模式?这是其他列的默认行为,我相信一般的 DataGrid。按 F2 启动编辑模式,但使用鼠标双击不会。

如果我将 ListView.IsEnabled 设置为 False,则双击有效,但是我有一个禁用的列表视图,它看起来不正确,并且任何样式 hack 都感觉像一个丑陋的杂物。

请注意,我尝试过single click editing,但没有成功。

任何帮助表示赞赏,谢谢!

【问题讨论】:

    标签: wpf datagrid celltemplate celleditingtemplate


    【解决方案1】:

    当然,只要我问 SO,答案就会实现 :) 如果我使用 single click editing trick 中的 FindVisualParent 方法并将其连接到列表视图,双击它会按预期工作:

    <DataTemplate x:Key="LimitsTemplate">
        <ListView ItemsSource="{Binding Limits}" PreviewMouseDoubleClick="limitsListView_PreviewMouseDoubleClick">
        ...
    

    在后面的代码中:

    static T FindVisualParent<T>(UIElement element) where T : UIElement
    {
        UIElement parent = element;
        while (parent != null)
        {
            T correctlyTyped = parent as T;
            if (correctlyTyped != null)
            {
                return correctlyTyped;
            }
    
            parent = System.Windows.Media.VisualTreeHelper.GetParent(parent) as UIElement;
        }
        return null;
    }
    
    void limitsListView_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        DataGrid dataGrid = FindVisualParent<DataGrid>(sender as UIElement);
        if (dataGrid != null)
        {
            dataGrid.BeginEdit();
        }
    }
    

    【讨论】:

      【解决方案2】:

      我的 DataGrid 遇到了非常相似的问题。以下是导致我的项目出现问题的原因:我的 DataGrid 中的 ItemsSource 被分配了一个实现 IEnumerable 的自定义列表。

      我实现了这个列表,以便它为同一索引的不同调用返回不同的对象.. 就像你第一次调用 list[0] 时它返回一个包含名称“WPF”的对象,例如如果你调用它再次 list[0] 它将为您返回一个包含值“WPF”的全新对象。

      因此,如果您要绑定的集合(限制)是您为其实现了 IEnumerable 和 IList 接口的自定义集合,那么请检查您的实现。在我的例子中,它是索引运算符,IndexOf 和 Contains。

      My Blog

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-22
        • 2013-08-20
        • 2013-11-16
        • 1970-01-01
        • 2010-12-20
        • 2013-09-13
        • 1970-01-01
        相关资源
        最近更新 更多