【问题标题】:DataGrid Row Header Templates and Row Validation issuesDataGrid 行标题模板和行验证问题
【发布时间】:2016-12-08 14:45:08
【问题描述】:

我有一个 WPF 应用程序,它使用 MVVM 设计模式和实体框架。在这个应用程序中,我有一个 Datagrid,它具有行验证,并且工作得很好。单元格有红色边框,Datagrid 行标题有红色!在里面,正是我想要的。

然后我希望能够双击行标题来执行一些操作,所以我有以下将事件绑定到我的 ViewModel

<DataGrid.RowHeaderTemplate>
    <DataTemplate>
        <ContentControl >
            <Label Content="   ">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseDoubleClick">
                        <cmd:EventToCommand 
                        Command="{Binding Main.SomeCommand, Source={StaticResource Locator}}" 
                        CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Label>
        </ContentControl>
    </DataTemplate>
</DataGrid.RowHeaderTemplate>

现在添加它会删除红色!在行标题上指示该行中的错误。

我现在无法弄清楚如何在行验证中显示错误以及让我的项目添加交互触发器以绑定命令。

无论我以自定义样式触发器或自定义 DataGrid RowValidationErrorTemplates 的方式添加什么,它都会被我的 RowHeaderTemplate 覆盖,我无法弄清楚如何将两者结合起来。

如何在 Datagrid 行标题上同时显示错误指示和交互触发器?

【问题讨论】:

    标签: wpf mvvm datagrid


    【解决方案1】:

    解决了,有点。

    根据http://www.codeproject.com/Articles/30905/WPF-DataGrid-Practical-Examples#validation

    创建样式

        <Style x:Key="RowStyle" TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="BorderThickness" Value="1"/>
                    <Setter Property="BorderBrush" Value="Red"/>
                    <Setter Property="ToolTip"
              Value="{Binding RelativeSource={RelativeSource Self},
                     Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    

    添加到数据网格

    <DataGrid.RowValidationRules>
        <local:RowDataInfoValidationRule ValidationStep="UpdatedValue" />
    </DataGrid.RowValidationRules>
    

    RowStyle="{StaticResource RowStyle}"
    

    public override ValidationResult Validate(object value,
                        CultureInfo cultureInfo)
        {
            BindingGroup group = (BindingGroup)value;
    
            StringBuilder error = null;
            foreach (var item in group.Items)
            {
                // aggregate errors
                IDataErrorInfo info = item as IDataErrorInfo;
                if (info != null)
                {
                    if (!string.IsNullOrEmpty(info.Error))
                    {
                        if (error == null)
                            error = new StringBuilder();
                        error.Append((error.Length != 0 ? ", " : "") + info.Error);
                    }
                }
            }
    
            if (error != null)
                return new ValidationResult(false, error.ToString());
    
            return ValidationResult.ValidResult;
        }
    

    最终结果是整行在整行级别上被标记为无效

    我仍然希望能够只影响行标题,所以我仍然会接受这个答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2011-05-16
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多