Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
4155
Error binding value constraint in DataTemplate style
posted

Hi, 

I have a regular WPF DataGrid with a template column whose content control style is set via a DataTrigger.  In the style I have a XamNumericEditor with a the value constraint that needs to be bound to the DataGridRow Item property which I will pass to a converter to get the minimum and maximum values.

I am getting the following error :

Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGridRow', AncestorLevel='1''. BindingExpression:Path=DataContext; DataItem=null; target element is 'ValueConstraint' (HashCode=22196637); target property is 'MinInclusive' (type 'Object')

Here is my setup with most unnecessary formatting stripped away.  Note that in the style the bound property "ParameterValue" does indeed bind correctly, however the ValueConstraint binding does not work properly.

<DataTemplate x:Key="DecimalParameter">

     <igEditor:XamNumericEditor  Mask="{Binding Converter={StaticResource ParameterToPrecisionMask}}" ValueType="{x:Type system:Decimal}" Value="{Binding ParameterValue, ValidatesOnDataErrors=True, UpdateSourceTrigger=LostFocus}">

          <igEditor:XamNumericEditor.ValueConstraint>

               <igEditor:ValueConstraint MinInclusive="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}, Path=DataContext, Converter={StaticResource ParameterToMinNumeric}}"/>

          </igEditor:XamNumericEditor.ValueConstraint>

     </igEditor:XamNumericEditor>

</DataTemplate>

<DataGrid HeadersVisibility="None" AutoGenerateColumns="False" ItemsSource="{Binding    Parameters}" GridLinesVisibility="None" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="False">

     <DataGrid.Columns>

          <DataGridTemplateColumn Width="*">

               <DataGridTemplateColumn.CellTemplate>

                    <DataTemplate>

                         <ContentControl Content="{Binding}" IsEnabled="{Binding IsEnabled}" IsTabStop="False">

                              <ContentControl.Style>

                                   <Style TargetType="ContentControl">

                                        <Style.Triggers>

                                             <DataTrigger Binding="{Binding DataTypeID}" Value="1

                                                  <Setter Property="ContentTemplate" Value="{StaticResource DecimalParameter}"/>

                                             </DataTrigger>

                                        </Style.Triggers>

                                   </Style>

                              </ContentControl.Style>

                         </ContentControl>

                    </DataTemplate>

               </DataGridTemplateColumn.CellTemplate>

          </DataGridTemplateColumn>

     </DataGrid.Columns>

</DataGrid>

How can I bind the style to the item in the DataGridRow's DataContext?

Thanks,

-Phillip

Parents Reply Children
No Data