【问题标题】:Change between DataGrids CellTemplate and EditingCellTemplate on button click inside EditingCellTemplate数据网格 CellTemplate 和 Editing CellTemplate on button click inside Editing CellTemplate 之间的更改
【发布时间】:2019-06-19 12:43:50
【问题描述】:

我正在使用DataGrid,并且在单击按钮时,我希望能够在DataGrid 列的CellTemplateEditingCellTemplate 之间进行切换。

显示 DataGrid

加载时,DataGrid 显示具有权限级别的CellTemplate

当用户在权限级别单元格内双击时,模板变为EditingCellTemplate 并出现ItemsControl 按钮。

显示按钮

当用户按下管理员、读取或写入这些按钮之一时,我希望权限级别模板显示 CellTemplate 仅显示文本而不是 EditingCellTemplate。 我考虑过使用一种行为,但不确定它是如何工作的。我的两个 CellTemplate 都在资源字典中。

显示文本的CellTemplate

<DataTemplate x:Key="PermissionTemplate">
    <Border>
        <Label  Content="{Binding Path=PermissionLevel.Access}" />
    </Border>
</DataTemplate>

编辑单元格模板

<DataTemplate x:Key="EditingPermissionTemplate">
    <Border>
        <UniformGrid Rows="1" Columns="1">
                <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl, Mode=FindAncestor}, Path=DataContext.AllPermissionLevels}" HorizontalContentAlignment="Stretch">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <Button Style="{StaticResource BaseButtonStyle}"
                                    Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl, Mode=FindAncestor}, Path=DataContext.UpdatePermissionCommand}"
                                        CommandParameter="{Binding}" >
                                    <TextBlock TextWrapping="Wrap" Text="{Binding Path=Access}" />
                                </Button>
                            </StackPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
        </UniformGrid>
    </Border>
</DataTemplate>

数据网格

<DataGrid ItemsSource="{Binding Path=AllUsersModules}" SelectedItem="{Binding Path=SelectedUsersModule}" 
                      Style="{StaticResource BaseDataGridStyle}" SelectionUnit="FullRow">
                <DataGrid.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="Background" Value="{StaticResource WhiteColorBrush}" />
                        <Setter Property="Foreground" Value="Black" />
                        <Style.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" Value="Transparent" />
                                <Setter Property="BorderBrush" Value="Orange" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.CellStyle>

                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Module" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" Width="*" 
                                            CellTemplate="{StaticResource ModuleTemplate}"/>
                    <DataGridTemplateColumn Header="Permission Level" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" Width="*" 
                                            CellTemplate="{StaticResource PermissionTemplate}" 
                                            CellEditingTemplate="{StaticResource EditingPermissionTemplate}"/>
                </DataGrid.Columns>
            </DataGrid>

【问题讨论】:

  • 是的,完全正确

标签: c# .net wpf datagrid


【解决方案1】:

如果您想在单击Button 时退出编辑模式,您可以连接一个调用DataGridCancelEdit() 方法的Click 事件处理程序。 Here 是如何在 ResourceDictionary 中执行此操作的。

private void Button_Click(object sender, RoutedEventArgs e)
{
    DataGrid dataGrid = FindParent<DataGrid>((Button)sender);
    if (dataGrid != null)
        dataGrid.CancelEdit();
}

private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
    var parent = VisualTreeHelper.GetParent(dependencyObject);

    if (parent == null) return null;

    var parentT = parent as T;
    return parentT ?? FindParent<T>(parent);
}

【讨论】:

    【解决方案2】:

    感谢@mm8,我创建了一个附加到我的按钮以关闭编辑模板的行为。

    public static class SwitchCellTemplate
        {
            /// <summary>
            /// Attached property to buttons to close host window
            /// </summary>
            public static readonly DependencyProperty SwitchTemplate =
                DependencyProperty.RegisterAttached
                (
                    "CloseTemplate",
                    typeof(bool),
                    typeof(SwitchCellTemplate),
                    new PropertyMetadata(false, SwithcTemplateChanged)
                );
    
            public static bool GetSwitchTemplateProperty(DependencyObject obj)
            {
                return (bool)obj.GetValue(SwitchTemplate);
            }
    
            public static void SetSwitchTemplateProperty(DependencyObject obj, bool value)
            {
                obj.SetValue(SwitchTemplate, value);
            }
    
    
            public static void SwithcTemplateChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
            {
                if (property is Button)
                {
                    Button button = property as Button;
                    if (button != null) button.Click += OnClick;
                }
    
            }
    
            private static void OnClick(object sender, RoutedEventArgs e)
            {
                if (sender is Button)
                {
                    DataGrid dataGrid = FindParent<DataGrid>((Button)sender);
                    if (dataGrid != null)
                        dataGrid.CancelEdit();
                }
            }
    
            private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
            {
                var parent = VisualTreeHelper.GetParent(dependencyObject);
    
                if (parent == null) return null;
    
                var parentT = parent as T;
                return parentT ?? FindParent<T>(parent);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2013-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 1970-01-01
      • 2013-09-24
      • 2017-08-19
      • 1970-01-01
      相关资源
      最近更新 更多