Click here to Skip to main content
15,041,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

I am new to wpf , i have datagrid has list of class and in its data template i had multiple radio button its values come from enum (using converter to bind them),
and ia had validation row rule , when iam not selecting radio button it return validate result (isvalid = false) but unfortunately error template not applied nothing visually says there's error in this row :(

here 's my datagrid markup
XML
<datagrid x:Name="dgServices" AutoGenerateColumns="False" Style="{StaticResource DataGridStyle}" Grid.Column="0" Grid.Row="1" >
       <datagrid.rowvalidationrules>
           <local:datagridvalidationrule validationstep="UpdatedValue" xmlns:local="#unknown" />
       </datagrid.rowvalidationrules>

       <datagrid.RowValidationErrorTemplate>
           <ControlTemplate targettype="{x:Type DataGridRow}">
               <Grid Margin="0,-2,0,-2" Removed="Red" HorizontalAlignment="Stretch" ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent}">
                   <TextBlock Text="!" FontSize="{TemplateBinding FontSize}" FontWeight="Bold" Foreground="White" HorizontalAlignment="Center 
                   </TextBlock>
            </ControlTemplate>
       </datagrid.RowValidationErrorTemplate>

                <datagrid.columns>
                    <datagridtextcolumn width="200" binding="{Binding Title}">
                        <datagridtextcolumn.elementstyle>
                            <Style TargetType="TextBlock">
                                <setter property="VerticalAlignment" value="Center"></setter>
                                <setter property="Margin" value="20,0,0,0"></setter>
                            </Style>
                        </datagridtextcolumn.elementstyle>
                    </datagridtextcolumn>

                    <datagridtemplatecolumn width="350">
                        <datagridtemplatecolumn.header>
                            <grid width="350">
                                <grid.columndefinitions>
                                    <columndefinition />
                                    <columndefinition />
                                    <columndefinition />
                                    <columndefinition />
                                </grid.columndefinitions>
                                <textblock grid.column="0" name="txtExcellent"
                                           Margin="10,0,0,0" FontFamily="Fonts/#MagmaIIW01LightItalic"  Text="{x:Static p:Resources.txtExcellent}"/>
                                <textblock grid.column="1" name="txtGood"
                                           Margin="15,0,0,0" FontFamily="Fonts/#MagmaIIW01LightItalic"  Text="{x:Static p:Resources.txtGood}"/>
                                <textblock grid.column="2" name="txtAverage" fontfamily="Fonts/#MagmaIIW01LightItalic" text="{x:Static p:Resources.txtAverage}" />
                                <textblock grid.column="3" name="txtPoor" fontfamily="Fonts/#MagmaIIW01LightItalic" text="{x:Static p:Resources.txtPoor}" />
                            </textblock></textblock>
                            </grid>
                        </datagridtemplatecolumn.header>
                        <!-- Header Nedded-->

                        <datagridtemplatecolumn.celltemplate>
                            <datatemplate>
                                <stackpanel orientation="Horizontal">
                                    <radiobutton margin="5,0,0,0" style="{StaticResource ExcellentButton}"
                                                     IsChecked="{Binding  Path=StatusOptions,TargetNullValue=(select status),ValidatesOnDataErrors=True,
                                                     Converter={StaticResource enumConverter},
                                             ConverterParameter={x:Static local:Status.excellent},
                                            Mode=TwoWay,UpdateSourceTrigger=LostFocus}" Content="Excellent"  />

                                    <radiobutton margin="40,0,0,0" style="{StaticResource GoodButton}"
                                                     IsChecked="{Binding Path=StatusOptions,ValidatesOnDataErrors=True,
                                                     Converter={StaticResource enumConverter},
                                             ConverterParameter={x:Static local:Status.good},Mode=TwoWay,UpdateSourceTrigger=LostFocus}"
                                                     Content="Good"/>

                                    <radiobutton margin="40,0,0,0" style="{StaticResource AverageButton}"
                                                     IsChecked="{Binding Path=StatusOptions,ValidatesOnDataErrors=True,
                                            Converter={StaticResource enumConverter},
                                    ConverterParameter={x:Static local:Status.average},
                                        Mode=TwoWay,UpdateSourceTrigger=LostFocus}" Content="Average"/>

                                    <radiobutton margin="40,0,0,0" style="{StaticResource PoorButton}"
                                                     IsChecked="{Binding Path=StatusOptions,ValidatesOnDataErrors=True,
                                                     Converter={StaticResource enumConverter},
                                             ConverterParameter={x:Static local:Status.poor},Mode=TwoWay,UpdateSourceTrigger=LostFocus}" 
                                                     Content="Poor" />
                                </radiobutton></radiobutton></radiobutton></radiobutton></stackpanel>
                            </datatemplate>
                        </datagridtemplatecolumn.celltemplate>
                    </datagridtemplatecolumn>
                </datagrid.columns>
            </datagrid>

and here is rule validator
C#
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            Services service = (value as BindingGroup).Items[0] as Services;

            if (!service.StatusOptions.HasValue)
            {
                return new ValidationResult(false,
                    "select status option");
            }
            else
            {
                return ValidationResult.ValidResult;
            }
        }

and here's my style
XML
<Style TargetType="DataGridRow">
      <setter property="Background" value="Yellow"></setter>
      <setter property="ValidationErrorTemplate">
          <setter.value>
              <controltemplate x:name="TextErrorTemplate" targettype="DataGridRow" xmlns:x="#unknown">
                  <dockpanel lastchildfill="True">
                      <adornedelementplaceholder>
                          <border borderbrush="Red" borderthickness="2" />
                      </adornedelementplaceholder>
                      <datagridrow removed="Red" />
                  </dockpanel>
              </controltemplate>
          </setter.value>
      </setter>
      <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}"/>
          </setter></trigger>
      </Style.Triggers>

  </Style>


What I have tried:

i tried all thing on the internet that talking about error template for row
Posted
Updated 22-Mar-16 2:23am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900