1

I am trying to bind the ItemsSource property of my DataGridComboBoxColumn to a property of my ViewModel. I understand that due to some limitations this is not possible without using a static list of some sort or a workaround.

I chose to go the workaround route. I tried every answer on this StackOverflow thread with no luck, and most recently I tried this workaround with no luck either.

Here is my XAML as per my last attempt:

<DataGridComboBoxColumn Header="Action"
                        SelectedValueBinding="{Binding DISPLAY_ACTION_ID, Mode=TwoWay}"
                        SelectedValuePath="DISPLAY_ACTION_ID"
                        DisplayMemberPath="DISPLAY_ACTION_DESC">
    <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding ActionSource}" />
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding ActionSource}" />
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

I have also tried adjusting the ItemsSource binding as per this thread with no luck.

Here is my ViewModel property in case it helps:

public List<DISPLAY_ACTION> ActionSource
{
    get
    {
        return _actionSource;
    }
}

All I am getting is an empty list of values. If I put a breakpoint on the property getter it never breaks, i.e. it seems like the XAML isn't finding the ActionSource binding. Any help or guidance on why this might not be working would be greatly appreciated.

Edit - Updated XAML:

<DataGrid HorizontalAlignment="Stretch"
          VerticalAlignment="Top"
          SelectionMode="Single"
          RowHeaderWidth="20"
          AutoGenerateColumns="False"
          AlternatingRowBackground="Transparent"
          ItemsSource="{Binding DisplayItemsView}">
    <DataGrid.Columns>
        <DataGridComboBoxColumn Header="Action"
                                SelectedValueBinding="{Binding DISPLAY_ACTION_ID, Mode=TwoWay}"
                                SelectedValuePath="DISPLAY_ACTION_ID"
                                DisplayMemberPath="DISPLAY_ACTION_DESC">
            <DataGridComboBoxColumn.ElementStyle>
                <Style TargetType="ComboBox">
                    <Setter Property="ItemsSource" Value="{Binding DataContext.ActionSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" />
                </Style>
            </DataGridComboBoxColumn.ElementStyle>
            <DataGridComboBoxColumn.EditingElementStyle>
                <Style TargetType="ComboBox">
                    <Setter Property="ItemsSource" Value="{Binding DataContext.ActionSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" />
                </Style>
            </DataGridComboBoxColumn.EditingElementStyle>
        </DataGridComboBoxColumn>
    </DataGrid.Columns>
</DataGrid>

Debugging Error:

System.Windows.Data Error: 4 : Cannot find source for binding with reference RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:Path=DataContext.ActionSource; DataItem=null; target element is 'TextBlockComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')`

4

1 回答 1

1

假设您DataGrid绑定到一个MyItem对象列表。你有两个选择:

a) 每个MyItem对象都包含填充ActionSource列表。

b) 您的主 ViewModel 包含MyItem对象列表(绑定到 DataGrid)和ActionSource列表。然后您应该将 ComboBoxItemsSource绑定更改为:

<Setter Property="ItemsSource" Value="{Binding DataContext.ActionSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" />

在这两种情况下,您都必须确保SelectedValueBinding指向对象的属性MyItem,而不是DISPLAY_ACTION对象。

于 2014-05-13T16:08:44.823 回答