1
votes

I've got a datagrid in WPF that shows a grid of some data. The data is retrieved form a ViewModel, which contains the following properties:

Public ReadOnly Property Devices() As List(Of Device)
    Get
        Return FDevices
    End Get

.

Public ReadOnly Property ClientNetworks() As List(Of network)
    Get
        Return fnetwork
    End Get
End Property

Both properties are filled with data after constructing the view model. To use the properties in the Datagrid i use the following XAML.

<DataGrid  ItemsSource="{Binding Devices}" AutoGenerateColumns="False" >
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Customer" >
                <DataGridTemplateColumn.CellTemplate >
                    <DataTemplate>
 ------------------   <TextBlock Text="{Binding ClientNetwork.Description}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
 ------------------>  <ComboBox ItemsSource="{Binding ClientNetwork}" DisplayMemberPath="Description"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
</DataGrid>

This should show a textbox with the description and a combobox with strings when edited.

Outside of the Datagrid the combobox works fine. I know this is because of the set ItemsSource on the Datagrid but i can't seem to find how to make it work. i've tried several alterations of the combobox code but none have worked so far.

The goal is to make the user be able to edit the cell and have a combobox presented, from which he can select an string, then the corresponding int will be saved in the database.

UPDATE 1

<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext.ClientNetworks}"
                                  DisplayMemberPath="Description"
                                  SelectedItem="{Binding ClientNetwork}"
                                  />

This is how i fixed the resting of the datacontext

1

1 Answers

0
votes

I found a way to get it done but i am not sure this is the way it should be done

<Window.Resources>
    <CollectionViewSource Source="{Binding ClientNetworks}" x:Key="clientnetworks" />
</Window.Resources>

and in the combobox

<ComboBox ItemsSource="{Binding Source={StaticResource clientnetworks}}" DisplayMemberPath="Description" />