简体   繁体   English

WPF DataGrid-如何在DataGridTemplateColumn中使用单元格和行验证

[英]WPF DataGrid - How do I use cell and row validation with DataGridTemplateColumn

How do I use cell and row validation with DataGridTemplateColumn? 如何在DataGridTemplateColumn中使用单元格和行验证?

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding DataType}"/>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <ComboBox SelectedItem="{Binding DataType}" ItemsSource="{Binding Source={x:Static app:ApplicationConfiguration.DataTypes}, ValidatesOnDataErrors=True}"/>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

It's a bit of a guess, but it looks like you want to prevent certain items from being selected. 有点猜测,但是您似乎想防止选择某些项目。 The easiest way would be to remove them from the list, but you could do it using validation as follows. 最简单的方法是将它们从列表中删除,但是您可以使用验证来进行以下操作。

If the selected item is invalid, throw an exception in the Setter in the ViewModel: 如果所选项目无效,则在ViewModel的Setter中引发异常:

public object DataType
{
    get { return dataType; }
    set
    {
        if(valueNotAllowed(value))
            throw new Exception(string.Format("{0} is not a valid selection", value.ToString());
        dataType = value;
    }
}

Then set the binding for SelectedItem to ValidateOnExceptions (note that in your question, you specified ValidatesOnErrors for the ItemsSource binding - wrong property on the wrong binding): 然后将SelectedItem的绑定设置为ValidateOnExceptions(请注意,在您的问题中,您为ItemsSource绑定指定了ValidatesOnErrors-错误的绑定上的错误属性):

<ComboBox SelectedItem="{Binding Path=DataType, ValidatesOnExceptions=True}" 
ItemsSource="{Binding Source={x:Static app:ApplicationConfiguration.DataTypes}}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在wpf DataGrid中单击DataGridTemplateColumn中的按钮时如何停止行选择 - How do I stop row selection when a button in DataGridTemplateColumn is clicked in wpf DataGrid 如何在wpf datagrid中获取DatagridTemplateColumn的单元格内容? - How to get the cell content of DatagridTemplateColumn in wpf datagrid? 如何使用wpf将多个控件添加到数据网格的DataGridTemplateColumn? - How do I add multiple controls to a DataGridTemplateColumn of a datagrid using wpf? WPF DataGrid 从 DataGridTemplateColumn 获取单元格内容 - WPF DataGrid Get Cell Content from DataGridTemplateColumn WPF DataGrid:如何使DataGridTemplateColumn中的所有按钮具有相同的图像? - WPF DataGrid: How do I get all buttons in a DataGridTemplateColumn to have the same image? 如何在WPF Toolkit DataGrid上对DataGridTemplateColumn进行排序? - How can I sort a DataGridTemplateColumn on a WPF Toolkit DataGrid? 使用 C# WPF DataGrid DataGridTemplateColumn 时将行而不是单元格绑定到转换器 - Bind row instead of cell to converter when using C# WPF DataGrid DataGridTemplateColumn 在WPF数据网格中,如何在单元格编辑后获得新行? - In a WPF datagrid, how do I get the new row after cell edit? 当 SelectionMode 设置为 Cell 时,如何突出显示 WPF DataGrid 中的一行 - How do I highlight a row in the WPF DataGrid when SelectionMode is set to Cell wpf datagrid单元验证 - wpf datagrid cell validation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM