我在绑定到ViewModel(VM)的WPF UserControl中有一个只读数据网格,并且有一个SelectedItem绑定到VM的属性。还有两个文本框,一个绑定到SelectedItem属性,另一个绑定到VM属性。
<UserControl
x:Class="PhoneAssistant.WPF.Features.ServiceRequests.ServiceRequestsMainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PhoneAssistant.WPF.Features.ServiceRequests"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DataContext="{d:DesignInstance Type=local:ServiceRequestsMainViewModel}"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid
Grid.Row="0"
Margin="16,20,0,15"
d:Background="LightCoral">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<TextBox
Grid.Column="0" Margin="0,0,5,0"
Text="{Binding SelectedSR.BoundToSelected}" />
<TextBox
Grid.Column="1" Margin="0,0,5,0"
Text="{Binding VmProperty}" />
</Grid>
<DataGrid
Grid.Row="1" AutoGenerateColumns="False"
IsReadOnly="true"
ItemsSource="{Binding ServiceRequests}"
SelectedItem="{Binding SelectedSR}"
SelectionMode="Single"SelectionUnit="FullRow">
<DataGrid.Columns>
<DataGridTextColumn
Width="auto"
Binding="{Binding BoundToSelected}"
Header="BoundToSelected" />
<DataGridTextColumn
Width="auto"
Binding="{Binding BoundToVMProperty}"
Header="BoundToVMProperty" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>
对绑定到SelectedItem属性的文本框所做的更改将反映回DataGrid,但对绑定到VM属性的文本框所做的更改不会反映回DataGrid。
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
namespace PhoneAssistant.WPF.Features.ServiceRequests;
public sealed partial class ServiceRequestsMainViewModel : ObservableObject, IServiceRequestsMainViewModel
{
public ObservableCollection<ServiceRequest> ServiceRequests { get; } = new();
[ObservableProperty]
private ServiceRequest _selectedSR;
partial void OnSelectedSRChanged(ServiceRequest value)
{
VmProperty = value.BoundToVMProperty;
}
[ObservableProperty]
// Neither of these notifies update the datagrid
[NotifyPropertyChangedFor(nameof(ServiceRequests))]
[NotifyPropertyChangedFor(nameof(SelectedSR))]
private string _vmProperty;
partial void OnVmPropertyChanged(string value)
{
SelectedSR.BoundToVMProperty = value;
}
public Task LoadAsync()
{
ServiceRequests.Add(new ServiceRequest() { Id = 1, BoundToSelected = "SomeValue1", BoundToVMProperty = "AnotherValee1"});
ServiceRequests.Add(new ServiceRequest() { Id = 2, BoundToSelected = "SomeValue2", BoundToVMProperty = "AnotherValee2" });
ServiceRequests.Add(new ServiceRequest() { Id = 3, BoundToSelected = "SomeValue3", BoundToVMProperty = "AnotherValee3" });
return Task.CompletedTask;
}
}
public sealed class ServiceRequest
{
public int Id { get; init; }
public string BoundToSelected { get; set; }
public string BoundToVMProperty { get; set; }
}
我如何获得这些更改以更新数据网格?
1条答案
按热度按时间lsmepo6l1#
将数据网格绑定到BoundToVMProperty,而将文本框绑定到VmProperty
更改文本框绑定