2010-07-05 2 views
2

Я пытаюсь использовать элемент управления DataGrid WPFToolkit (и C# /. Net 3.5) для отображения ComboBox на запись. С ниже коде на ComboBoxes появляются, но их выпадающие списки не содержат элементов:Binding DataGridComboBoxColumn ItemsSource to RelativeSource FindAncestor не работает

<wpftkit:DataGrid ItemsSource="{Binding TransactionToEdit.SisterTransactions}" 
      AutoGenerateColumns="False"> 
<wpftkit:DataGrid.Columns> 
    <wpftkit:DataGridComboBoxColumn Header="Account" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}, diagnostics:PresentationTraceSources.TraceLevel=High}, Path=DataContext.Accounts}" DisplayMemberPath="Name"/> 
</wpftkit:DataGrid.Columns> 
</wpftkit:DataGrid> 

Кроме того, окно вывода Visual Studio показывает следующее сообщение об ошибке:

System.Windows.Data Error: 4 : Cannot find source for binding with 
    reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.StackPanel', AncestorLevel='1''. 
    BindingExpression:Path=DataContext.Accounts; DataItem=null; target element is 
    'DataGridComboBoxColumn' (HashCode=25733404); target property is 
    'ItemsSource' (type 'IEnumerable') 

Однако следующий код работает как ожидается (падение в ComboBoxes' списках правильно заполняется):

<ItemsControl ItemsSource="{Binding TransactionToEdit.SisterTransactions}"> 
<ItemsControl.ItemTemplate> 
    <DataTemplate> 
    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=DataContext.Accounts, diagnostics:PresentationTraceSources.TraceLevel=High}" DisplayMemberPath="Name"/> 
    </DataTemplate> 
</ItemsControl.ItemTemplate> 
</ItemsControl> 

Обратите внимание, что как DataGrid и ItemsControl имеют одинаковые строки ItemsSource. Так же делают DataGridComboBoxColumn и ComboBox. Один элемент управления связывается правильно, а другой - нет.

Почему данные DataGridComboBoxColumn ItemsSource не связываются правильно?

Спасибо,
Бену

FYI, diagnostics определяется как xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"

+0

У меня такая же проблема !! DataGridComboBoxColumn - это просто испорченная вещь! – Shimmy

ответ

3

Интересно ... если я создаю пользовательский DataGridColumn, содержащий ComboBox и использовать тот же ItemsSource связывающую строку, как указано выше , оно работает.

<wpftkit:DataGridTemplateColumn.CellTemplate> 
    <DataTemplate> 
    <ComboBox SelectedItem="{Binding Account}" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=DataContext.Accounts}"  DisplayMemberPath="Name" /> 
    </DataTemplate> 
</wpftkit:DataGridTemplateColumn.CellTemplate> 
+1

Я обнаружил, что если я поставлю ComboBox в CellTemplate, могут произойти странные вещи ... например, ComboBox потеряет свое выбранное значение, когда строка переключится в режим редактирования. Простое исправление для этого заключалось в том, чтобы CellTemplate отображал TextBlock, а затем размещал ComboBox в CellEditingTemplate. –

+1

Это ошибка, она была поднята –

Смежные вопросы