发生验证错误时,如何将图像放在DataGridCell中?

时间:2017-03-02 21:39:44

标签: c# wpf wpfdatagrid

以下是我的DataGrid xaml标记当前的样子,即使发生验证错误,图像也根本不显示。我现有的唯一验证错误是,如果在字段中输入了数据(需要字段)。

<DataGrid RowValidationErrorTemplate="{x:Null}" Name="dataGridInstitution" ItemsSource="{Binding Path=OrgIDs, Mode=TwoWay}" CanUserDeleteRows="True" CanUserAddRows="True" AddingNewItem="DataGrid_AddingNewInstitution" AutoGenerateColumns="False" Height="300" Width="1170" CanUserResizeColumns="False" >
    <DataGrid.Resources>
        <Style x:Key="CellErrorStyle" TargetType="{x:Type DataGridCell}">
            <Setter Property="Validation.ErrorTemplate" >
                <Setter.Value>
                    <ControlTemplate>
                        <DataGridCell>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Path=(Validation.Errors)[0].ErrorContent}" />
                                <Image Source="icons/status_error.png" Height="16" HorizontalAlignment="Right" />
                            </StackPanel>
                        </DataGridCell>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTextColumn     Header="Institution ID" Binding="{Binding Institution, Mode=TwoWay, ValidatesOnDataErrors=True }"       Width="80"  CellStyle="{StaticResource CellErrorStyle}" />
        <DataGridTextColumn     Header="Org ID"         Binding="{Binding value, Mode=TwoWay, ValidatesOnDataErrors=True }"             Width="50"  CellStyle="{StaticResource CellErrorStyle}" />
        <DataGridTextColumn     Header="Data Source"    Binding="{Binding Datasource, Mode=TwoWay }"                                    Width="150" />
        <DataGridTextColumn     Header="User Code"      Binding="{Binding Security.UserCode, Mode=TwoWay, ValidatesOnDataErrors=True }" Width="70"  CellStyle="{StaticResource CellErrorStyle}" />
        <DataGridTextColumn     Header="Password"       Binding="{Binding Security.Password, Mode=TwoWay, ValidatesOnDataErrors=True }" Width="75"  CellStyle="{StaticResource CellErrorStyle}" />
        <DataGridComboBoxColumn Header="Image Web Service Location"           ItemsSource="{Binding Source={StaticResource Specifications}, Path=ImageWebServices, Mode=TwoWay}"           SelectedItemBinding="{Binding ServerLocation, ValidatesOnDataErrors=True}" Width="325" CellStyle="{StaticResource CellErrorStyle}" />
        <DataGridComboBoxColumn Header="Image Streaming Web Service Location" ItemsSource="{Binding Source={StaticResource Specifications}, Path=ImageStreamingWebServices, Mode=TwoWay }" SelectedItemBinding="{Binding StreamingServerLocation}"                    Width="325" />
        <DataGridComboBoxColumn Header="Trace Level"                          ItemsSource="{Binding Source={StaticResource tracingEnum}}"                                                  SelectedItemBinding="{Binding Tracing.Level, Mode=TwoWay}"                 Width="*" />
    </DataGrid.Columns>
</DataGrid>

1 个答案:

答案 0 :(得分:0)

你不应该这样做:RowValidationErrorTemplate="{x:Null}"

相反,您应该将其值设置为您希望控件在出现验证错误时所具有的期望值。像这样:

<DataGrid Name="dataGridInstitution" ItemsSource="{Binding Path=OrgIDs, Mode=TwoWay}" CanUserDeleteRows="True" CanUserAddRows="True" AddingNewItem="DataGrid_AddingNewInstitution" AutoGenerateColumns="False" Height="300" Width="1170" CanUserResizeColumns="False" >
    <DataGrid.RowValidationErrorTemplate>
      <ControlTemplate>
        <Grid Margin="0,-2,0,-2"
          <Image Source="myImage.jpg" />
        </Grid>
      </ControlTemplate>
    </DataGrid.RowValidationErrorTemplate>
    <DataGrid.Resources>
        <Style x:Key="CellErrorStyle" TargetType="{x:Type DataGridCell}">
            <Setter Property="Validation.ErrorTemplate" >
                <Setter.Value>
                   <ControlTemplate>
                       <DataGridCell>
                          <StackPanel Orientation="Horizontal">
                              <TextBlock Text="{Binding Path=(Validation.Errors)[0].ErrorContent}" />
                              <Image Source="icons/status_error.png" Height="16" HorizontalAlignment="Right" />
                          </StackPanel>
                       </DataGridCell>
                   </ControlTemplate>
               </Setter.Value>
             </Setter>
          </Style>
    </DataGrid.Resources>
    <DataGrid.Columns>
       <DataGridTextColumn     Header="Institution ID" Binding="{Binding Institution, Mode=TwoWay, ValidatesOnDataErrors=True }"       Width="80"  CellStyle="{StaticResource CellErrorStyle}" />
       <DataGridTextColumn     Header="Org ID"         Binding="{Binding value, Mode=TwoWay, ValidatesOnDataErrors=True }"             Width="50"  CellStyle="{StaticResource CellErrorStyle}" />
       <DataGridTextColumn     Header="Data Source"    Binding="{Binding Datasource, Mode=TwoWay }"                                    Width="150" />
       <DataGridTextColumn     Header="User Code"      Binding="{Binding Security.UserCode, Mode=TwoWay, ValidatesOnDataErrors=True }" Width="70"  CellStyle="{StaticResource CellErrorStyle}" />
       <DataGridTextColumn     Header="Password"       Binding="{Binding Security.Password, Mode=TwoWay, ValidatesOnDataErrors=True }" Width="75"  CellStyle="{StaticResource CellErrorStyle}" />
       <DataGridComboBoxColumn Header="Image Web Service Location"           ItemsSource="{Binding Source={StaticResource Specifications}, Path=ImageWebServices, Mode=TwoWay}"           SelectedItemBinding="{Binding ServerLocation, ValidatesOnDataErrors=True}" Width="325" CellStyle="{StaticResource CellErrorStyle}" />
       <DataGridComboBoxColumn Header="Image Streaming Web Service Location" ItemsSource="{Binding Source={StaticResource Specifications}, Path=ImageStreamingWebServices, Mode=TwoWay }" SelectedItemBinding="{Binding StreamingServerLocation}"                    Width="325" />
       <DataGridComboBoxColumn Header="Trace Level"                          ItemsSource="{Binding Source={StaticResource tracingEnum}}"                                                  SelectedItemBinding="{Binding Tracing.Level, Mode=TwoWay}"                 Width="*" />
  </DataGrid.Columns>

- 编辑 -

之前的方法是为每行提供验证错误,这有点标准且更有效。但是,如果你想逐个单元地验证,那么你需要使用这样的样式:

<Style TargetType="ContentPresenter">
    <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
</Style>
<Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
        <ControlTemplate >
            <!--You do your styling here-->
        </ControlTemplate>
    </Setter.Value>
</Setter>

有关详细信息,请参阅这篇精彩文章here