从视图到视图模型和文本块WPF Datagrid选定行

ni65a41a  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(137)

我在一些文本框下面有一个数据网格。我不知道如何将数据网格中的选定行转移到MVVM中的文本块和视图模型中。

<StackPanel>
            <ScrollViewer Grid.Row="2" Grid.Column="1" Height="300" Width="auto">
                <DataGrid x:Name="dtGrd" Width="800" AutoGenerateColumns="False" Background="Azure" ItemsSource="{Binding Path=Cars, Mode=TwoWay}" IsReadOnly="True" SelectionMode="Single">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Id" Width="auto" Binding="{Binding Path=Id}"/>
                        <DataGridTextColumn Header="Brand" Width="auto" Binding="{Binding Path=Brand}"/>
                        <DataGridTextColumn Header="Model" Width="auto" Binding="{Binding Path=Model}"/>
                        <DataGridTextColumn Header="Fuel" Width="auto" Binding="{Binding Path=Fuel}"/>
                        <DataGridTextColumn Header="MaxPassenger" Width="auto" Binding="{Binding Path=MaxPassenger}"/>
                        <DataGridTextColumn Header="NumberOfDoors" Width="auto" Binding="{Binding Path=NumberOfDoors}"/>
                        <DataGridTextColumn Header="AvailableType" Width="auto" Binding="{Binding Path=AvailableType}"/>
                        <DataGridTextColumn Header="Price" Width="auto" Binding="{Binding Path=Price}"/>
                    </DataGrid.Columns>
                </DataGrid>
            </ScrollViewer>
        </StackPanel>
        <Grid Margin="5" Height="150">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="225"/>
                <ColumnDefinition Width="225"/>
                <ColumnDefinition Width="225"/>
                <ColumnDefinition Width="225"/>
            </Grid.ColumnDefinitions>
            <StackPanel Grid.Column="0" Orientation="Vertical">
                <Label Content="ID" HorizontalAlignment="Center"/>
                <!-- <TextBox x:Name="txtBxId" Padding="5" Margin="5" Text="{Binding Path=CurrentCar.Id, Mode=TwoWay}"/> -->
                <TextBox x:Name="txtBxId" Padding="5" Margin="5" Text="{Binding Path=SelectedItem.Id, ElementName=dtGrd, Mode=TwoWay}"/>
                <Label Content="Brand" HorizontalAlignment="Center"/>
                <TextBox x:Name="txtBxBrand" Padding="5" Margin="5" Text="{Binding Path=SelectedItem.Brand, ElementName=dtGrd, Mode=TwoWay}"/>
            </StackPanel>
            <StackPanel Grid.Column="1" Orientation="Vertical">
                <Label Content="Model" HorizontalAlignment="Center"/>
                <TextBox x:Name="txtBxModel" Padding="5" Margin="5" Text="{Binding Path=SelectedItem.Model, ElementName=dtGrd, Mode=TwoWay}"/>
                <Label Content="Fuel" HorizontalAlignment="Center"/>
                <TextBox x:Name="txtBxFuel" Padding="5" Margin="5" Text="{Binding Path=SelectedItem.Fuel, ElementName=dtGrd, Mode=TwoWay}"/>
            </StackPanel>
            <StackPanel Grid.Column="2" Orientation="Vertical">
                <Label Content="MaxPassanger" HorizontalAlignment="Center"/>
                <TextBox x:Name="txtBxMaxPassanger" Padding="5" Margin="5" Text="{Binding Path=SelectedItem.MaxPassenger, ElementName=dtGrd, Mode=TwoWay}"/>
                <Label Content="NumberOfDoors" HorizontalAlignment="Center"/>
                <TextBox x:Name="txtBxNumberOfDoors" Padding="5" Margin="5" Text="{Binding Path=SelectedItem.NumberOfDoors, ElementName=dtGrd, Mode=TwoWay}"/>
            </StackPanel>
            <StackPanel Grid.Column="3" Orientation="Vertical">
                <Label Content="AvailableType" HorizontalAlignment="Center"/>
                <TextBox x:Name="txtBxAvailableType" Padding="5" Margin="5" Text="{Binding Path=SelectedItem.AvailableType, ElementName=dtGrd, Mode=TwoWay}"/>
                <Label Content="Price" HorizontalAlignment="Center"/>
                <TextBox x:Name="txtBxPrice" Padding="5" Margin="5" Text="{Binding Path=SelectedItem.Price, ElementName=dtGrd, Mode=TwoWay}"/>
            </StackPanel>
        </Grid>

我发现SelectedItem绑定会将所选行数据绑定到文本框,但我无法将其传输到viewmodel。
我尝试了一些随机论坛的方法,但没有一个工作,视图模型没有从视图中获得任何数据。

fumotvh3

fumotvh31#

我会将DataGrid的SelectedItem绑定到ViewModel:

<DataGrid (...) SelectedItem="{Binding SelectedCar, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" > (...)

如果CanUsersAddRows保持false,这应该就是您所需要的。否则,当用户编辑新车时会有一个小问题。然后,您需要添加一个转换器,以忽略最终出现在网格末尾的空白行。您可以在here或其他地方找到转换器实现。

相关问题