msturek
msturek

Reputation: 541

Binding an XceedData Column EditTemplate to different property

I have an Xceed DataGrid with an EditTemplate defined for the grid.

The grid shows a listing of items bound to a collection with about 6 columns and the EditTemplate is a TextBox control for entering a quantity. I want the IsReadOnly property to be bound to a different property so that the item has a serial number, IsReadOnly will be set to true so the user cannot enter a value. I want to bind to the SerialNum property within the same collection and pass it to a converter to return the true/false value. I've got the converter written; however, I'm having issues binding to the property to pass to the converter.

My DataGridCollectionViewSource is simple enough:

<xcdg:DataGridCollectionViewSource x:Key="transferItems" Source="{Binding TransferItems}" />

TransferItems is set in my ViewModel and all of the columns get properly bound.

For all of my generic display columns, they are being properly displayed via:

<xcdg:Column Title="Serial No." AllowSort="False" FieldName="SerialNum" />

My issue is in defining the xcgd:CellEditor template and I'm pretty sure my issue is around the RelativeSource. I've tried many different combinations trying to get the TransferItems.SerialNum property from my ViewModel, but no combination is working.

This is what I currently have:

<xcdg:Column Title="Xfer Qty Good" TextWrapping="Wrap" ReadOnly="False" Width="50" AllowGroup="False" AllowSort="False" FieldName="TransferQtyGood">
    <xcdg:Column.CellEditor>
        <xcdg:CellEditor>
            <xcdg:CellEditor.EditTemplate>
                <DataTemplate>
                    <TextBox x:Name="QtyGood" Margin="2,2,2,2" Width="50" HorizontalAlignment="Center" VerticalAlignment="Center"
                    Text="{xcdg:CellEditorBinding}" IsReadOnly="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type xcdg:DataGridCollectionViewSource}}, Path=DataContext.TransferItems.SerialNum, Converter={StaticResource serialToEnabledConverter}}"
                     />
                </DataTemplate>
            </xcdg:CellEditor.EditTemplate>
        </xcdg:CellEditor>
    </xcdg:Column.CellEditor>
</xcdg:Column>

Which gives the runtime error:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Xceed.Wpf.DataGrid.DataGridCollectionViewSource', AncestorLevel='1''. BindingExpression:Path=DataContext.TransferItems.SerialNum; DataItem=null; target element is 'TextBox' (Name='QtyGood'); target property is 'IsReadOnly' (type 'Boolean')

I understand what the error is telling me, but I'm just getting the proper RelativeSource path. I've read some of the helpful posts here on the enumerations for RelativeSource and still am missing something.

Upvotes: 2

Views: 655

Answers (1)

msturek
msturek

Reputation: 541

Just in case someone is having this issue, I was able to finally get the binding working in this manner. The key is defining the correct RelativeSource path:

<TextBox x:Name="QtyGood" Margin="2,2,2,2" Width="50" HorizontalAlignment="Center" VerticalAlignment="Center"
                                     Text="{xcdg:CellEditorBinding}" GotFocus="Qty_GotFocus" LostFocus="Qty_LostFocus"
                                     IsReadOnly="{Binding RelativeSource={RelativeSource AncestorType={x:Type xcdg:DataRow}}, Path=DataContext.SerialNum, Converter={StaticResource serialToEnabledConverter}}"
                                     />

Upvotes: 3

Related Questions