开发者

WPF, MVVM datagrid row binding

开发者 https://www.devze.com 2023-03-19 08:24 出处:网络
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\"

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>
                <DataGrid开发者_如何学PythonTextColumn 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? 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? P.S. It isnt because of some spell mistake, if there are spelling mistakes is because i renamed methods when I wrote the question etc.

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). It works the way I need it to until I try to bind the SelectedView attribute?

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 :-

<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:

<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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消