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>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
How can I bind the style to the item in the DataGridRow's DataContext?
Thanks,
-Phillip
Hi Phillip,
Thank you for your post. The XamNumericEditor’s ValueConstraint object is not present in the visual tree and this is why you cannot use binding in its properties. The WPF framework will not be able to determinate the source for the bindings. What you can do in this case is to bind the whole value constraint object with a converter and set the value constraint object’s properties in the converter’s code. I am attaching a sample based on your code which demonstrates the approach I am describing.
Please do not hesitate to let me know if you have any further questions on this matter.
Sincerely,
Radko Kolev
Infragistics Inc.
www.infragistics.com/support
Radko,
Thank you for the reply and the sample application. When I set a value for the value constraint as in the provided example without the binding the control prevents me from entering a value beyond the given range but in the example you provided I can enter values outside of the range but I receive a validation error. Is there any way to get the desired behavior of preventing a value from being entered beyond the range with binding so the value constraint can be used in a data template?
In the attached example you cannot enter a value outside of the range 0-100