2017-03-17 51 views
0

我有一個爲網格定義的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枚舉的有用帖子,但仍然缺少一些東西。

回答

0

爲了防止有人遇到此問題,我終於能夠以這種方式獲得綁定工作。關鍵是定義正確的RelativeSource路徑:

<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}}" 
            /> 
相關問題