Binding SpinEdit Value to the parent element from the DataTemplate in WPF

146 Views Asked by At

I'm using WPF and dotnet 7 with Devexpress Controls (22), what I'm trying to achieve is to show some data inside my GridControl (TableView):

public class First
{
   
    public required string Name { get; set; }

    public virtual List<Second> Seconds { get; set; }
}

public class Second
{
    public int Plus { get; set; }
}

my XAML is as follows:

<dxg:GridControl Name="MainGrid" AutoGenerateColumns="AddNew">
    <dxg:GridControl.View>
        <dxg:TableView Name="FirstTable" NewItemRowPosition="Bottom"  ShowGroupPanel="False" SearchPanelAllowFilter="True" SearchPanelHighlightResults="True"/>
    </dxg:GridControl.View>
    <dxg:GridControl.DetailDescriptor>
        <dxg:DataControlDetailDescriptor Margin="10" d:DataContext="{d:DesignInstance Type=models:First}" ItemsSourceBinding="{Binding Seconds}">
            <dxg:DataControlDetailDescriptor.DataControl>
                <dxg:GridControl x:Name="SecondGrid" AutoGenerateColumns="AddNew">
                    <dxg:GridControl.View>
                        <dxg:TableView x:Name="SecondTable" NewItemRowPosition="Bottom" ShowGroupPanel="False" SearchPanelAllowFilter="True" SearchPanelHighlightResults="True">
                            <dxg:TableView.FormatConditions>
                                <dxg:FormatCondition Expression="true">
                                    <dx:Format Foreground="SlateGray"/>
                                </dxg:FormatCondition>
                            </dxg:TableView.FormatConditions>
                        </dxg:TableView>
                    </dxg:GridControl.View>
                    <dxg:GridColumn FieldName="Plus" Binding="{Binding Plus}" d:DataContext="{d:DesignInstance models:Second}">
                        <dxg:GridColumn.CellTemplate>
                            <DataTemplate>
                                <dxe:SpinEdit HorizontalAlignment="Stretch"  
                                               Mask="##0"  
                                               AllowSpinOnMouseWheel="True"
                                               EditValueType="{x:Type system:Int32}"
                                               EditValue="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dxg:GridColumn}},Path=DataContext.Plus}"
                                               Increment="1"
                                               MinValue="0"/>
                            </DataTemplate>
                        </dxg:GridColumn.CellTemplate>
                    </dxg:GridColumn>
                </dxg:GridControl>
            </dxg:DataControlDetailDescriptor.DataControl>
        </dxg:DataControlDetailDescriptor>
    </dxg:GridControl.DetailDescriptor>
</dxg:GridControl>

as you can see its a nested Grid, which works fine, kinda..

the problem is that I CANNOT bind the plus value to the SpinEdit, I tried different ways but so far nothing worked, the value is actually inside the <dxg:GridColumn FieldName="Plus" .., so if I completely remove the DataTemplate it shows the value successfully, but it fails to bind it to the SpinEdit.

the FindAncestor should fetch the parent data but it fails with

Cannot find source: RelativeSource FindAncestor, AncestorType='DevExpress.Xpf.Grid.GridColumn', AncestorLevel='1'.

also by using TemplatedParent instead of FindAncestor, I got this exception:

System.NotSupportedException: 'Serialization and deserialization of 'System.Reflection.MethodBase' instances are not supported. Path: $.TargetSite.'

what is my mistake here?

UPDATE:

I did this:

 <dxg:GridColumn FieldName="Plus" x:Name="PlusColumn" d:DataContext="{d:DesignInstance models:Second}">
     <dxg:GridColumn.CellTemplate>
         <DataTemplate>
             <dxe:SpinEdit HorizontalAlignment="Stretch"  
                            Mask="##0"  
                            AllowSpinOnMouseWheel="True"
                            EditValue="{Binding ElementName=PlusColumn,Path=DataContext.Plus}"
                            Increment="1"
                            MinValue="0"/>
         </DataTemplate>
     </dxg:GridColumn.CellTemplate>
 </dxg:GridColumn>

and I got no binding failure and no error, BUT it still doesn't show the value, I even replaced the SpinEdit just to make sure its not the Control Problem, still nothing is shown.

0

There are 0 best solutions below