Pergunta

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')`

Foi útil?

Solução

Assume your DataGrid is bound to a list of MyItem objects. You have two options:

a) Each MyItem object contains filled ActionSource list.

b) Your main ViewModel contains both list of MyItem objects (bound to DataGrid) and the ActionSource list. Then you should change the ComboBox ItemsSource binding to:

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

In both cases you have to ensure the SelectedValueBinding points to a property of MyItem object, not DISPLAY_ACTION object.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top