简体   繁体   English

WPF,MVVM 数据网格行绑定

[英]WPF, MVVM datagrid row binding

I am wandering if anyone could help me work out the binding issues I am having?如果有人可以帮助我解决我遇到的绑定问题,我正在徘徊?

Snippets of Code:代码片段:

<DataGrid AutoGenerateColumns="False" Grid.Column="1" Grid.Row="1" SelectionMode="Single" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding PersonList}" CanUserSortColumns="True" SelectedItem="{Binding Path=SelectedPerson}" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="FirstName" Width="100" Binding="{Binding FirstName}" />
                <DataGridTextColumn Header="LastName" Width="100" Binding="{Binding LastName}" />
                <DataGridTemplateColumn Width="140" Header="Operator">
                    <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                        <ComboBox ItemsSource="{Binding Path=OperatorList}" DisplayMemberPath="FullName" SelectedValue="{Binding Path=SelectedOperator}" />
                            </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn> 
                ...
</DataGrid>

Above is a snippet of code from the view:上面是视图中的一段代码:

Each person in the list is its own viewmodel that has the code snippet below:列表中的每个人都是其自己的视图模型,其代码片段如下:

CollectionView _operatorList;
public CollectionView DebtorAgentList { get { return _operatorList; } }

Model.Operator _selectedOperator;
public Model.Operator SelectedOperator
{...}

Now the problem I am having is that the SelectedValue binding isn't working and I can't work out why?现在我遇到的问题是 SelectedValue 绑定不起作用,我不知道为什么? But what makes it tricky or different (maybe) is that every row in the data grid has its own viewmodel, so in otherwords a datagrid of viewmodels.但是让它变得棘手或不同(也许)的是数据网格中的每一行都有自己的视图模型,因此换句话说就是视图模型的数据网格。 So what is happening is that FirstName and LastName and Combobox are all filled correctly but I can't seem to get the SelectedValue to bind?那么发生了什么是 FirstName 和 LastName 以及 Combobox 都正确填写但我似乎无法让 SelectedValue 绑定? PS It isnt because of some spell mistake, if there are spelling mistakes is because i renamed methods when I wrote the question etc. PS这不是因为一些拼写错误,如果有拼写错误是因为我在写问题时重命名了方法等。

Further Details:更多详细信息:

The above Datagrid is part of a view that has its own viewmodel, this view model fills the datagrid above with a list of people, each person is a viewmodel in essence( well it isn't really a viewmodel but then again its more of a viewmodel than a plane model).上面的数据网格是具有自己的视图模型的视图的一部分,这个视图 model 用人员列表填充上面的数据网格,每个人本质上都是一个视图模型(它不是真正的视图模型,但它又更像是一个viewmodel 比平面模型)。 It works the way I need it to until I try to bind the SelectedView attribute?在我尝试绑定 SelectedView 属性之前,它会按照我需要的方式工作吗?

Could someone please tell me why that binding might not working?有人可以告诉我为什么该绑定可能不起作用吗?

Thanks In advance:D先谢谢了

Use SelectedValuePath and then use 'SelectedValue' to select any item in the collection, see the following code:-使用 SelectedValuePath 然后使用 'SelectedValue' 到 select 集合中的任何项目,请参见以下代码:-

<ComboBox ItemsSource="{Binding Path=OperatorList}" DisplayMemberPath="FullName" SelectedValuePath="SelectedOperator" SelectedValue="{Binding SelectedOperator}" />   

I found this article that helped me work it out:) instead of using a template column I used the DataGridComboBoxColumn, as below:我发现这篇文章帮助我解决了问题:) 我使用了 DataGridComboBoxColumn 而不是使用模板列,如下所示:

<DataGrid AutoGenerateColumns="False" Grid.Column="1" Grid.Row="1" SelectionMode="Single" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding PersonList}" CanUserSortColumns="True" SelectedItem="{Binding Path=SelectedPerson}" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="FirstName" Width="100" Binding="{Binding FirstName}" />
            <DataGridTextColumn Header="LastName" Width="100" Binding="{Binding LastName}" />
            <DataGridComboBoxColumn Header="Operator" DisplayMemberPath="FullName" Width="150" SelectedValueBinding="{Binding Path=SelectedOperator}" >
                <DataGridComboBoxColumn.ElementStyle>
                <Style TargetType="ComboBox">
                    <Setter Property="ItemsSource" Value="{Binding Path=OperatorList}" />
                </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding Path=OperatorList}" />
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>
            ...
</DataGrid>

The rest stayed the same, Thanks all:) rest 保持不变,谢谢大家:)

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

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