简体   繁体   English

具有数据绑定的WPF Datagrid,更改ItemsSource

[英]WPF Datagrid with databinding, changing ItemsSource

I've got a datagrid in WPF that shows a grid of some data. 我在WPF中有一个datagrid,它显示一些数据的网格。 The data is retrieved form a ViewModel, which contains the following properties: 从ViewModel检索数据,该ViewModel包含以下属性:

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中的属性,我使用以下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. 在Datagrid之外,组合框可以正常工作。 I know this is because of the set ItemsSource on the Datagrid but i can't seem to find how to make it work. 我知道这是因为在Datagrid上设置了ItemsSource,但我似乎找不到如何使它工作的方法。 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. 目的是使用户能够编辑单元格并显示一个组合框,从中可以选择一个字符串,然后将相应的int保存在数据库中。

UPDATE 1 更新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 这就是我修复数据上下文其余部分的方式

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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM