我有一個爲網格定義的EditTemplate
Xceed DataGrid。將XceedData列EditTemplate綁定到不同的屬性
網格顯示綁定到具有約6列的集合的項目列表,並且EditTemplate
是用於輸入數量的TextBox
控件。我希望IsReadOnly
屬性被綁定到不同的屬性,以便該項目具有序列號,IsReadOnly
將設置爲true,以便用戶無法輸入值。我想綁定到相同集合中的SerialNum
屬性,並將其傳遞給轉換器以返回true/false值。我已經寫好了轉換器。然而,我有問題綁定到屬性傳遞給轉換器。
我DataGridCollectionViewSource
很簡單:
<xcdg:DataGridCollectionViewSource x:Key="transferItems" Source="{Binding TransferItems}" />
TransferItems
在我的視圖模型設定和所有列的得到正確的約束。
對於我所有的通用顯示列的,他們正在通過正確顯示:
<xcdg:Column Title="Serial No." AllowSort="False" FieldName="SerialNum" />
我的問題是在定義xcgd:CellEditor
模板,我敢肯定我的問題是RelativeSource
左右。我嘗試了很多不同的組合,試圖從我的ViewModel獲得TransferItems.SerialNum
屬性,但沒有任何組合正在工作。
這是我目前有:
<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>
這給運行時錯誤:
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')
我明白了什麼錯誤是告訴我,但我只是得到適當的RelativeSource
路徑。我已經閱讀了一些關於RelativeSource
枚舉的有用帖子,但仍然缺少一些東西。