AVALONIA:添加行时刷新DataGrid无法正常工作



我已经尝试过用多种方式重写它。我每次都有同样的错误行为。我使用的是Avaloni版本:0.10.10代码如下所示:主窗口.axaml:

<Button Content="Add New Device" Margin="5" Command="{Binding AddNewDevice}"/>
<DataGrid Items="{Binding Devices}" Grid.Row="1">
<DataGrid.Columns>
<DataGridTextColumn Header=" IpAddress"
Binding="{Binding IpAddress}"/>
<DataGridTextColumn Header=" Type"
Binding="{Binding Type}"/>
<DataGridTextColumn Header="Dpi"
Binding="{Binding Dpi}"/>
</DataGrid.Columns>
</DataGrid>

MainWindowViewModel.cs:

[DataContract]
public class MainWindowViewModel : ViewModelBase
{
public ReactiveCommand<Unit, Unit> AddNewDevice { get; }
[Reactive]
public ObservableCollection<Device> Devices { get; set; }
public MainWindowViewModel()
{
Devices = new ObservableCollection<Device>(new GetSettingsService().GetSettings().Devices);
AddNewDevice = ReactiveCommand.Create(() =>
{
Devices.Add(
new Device()
{
IpAddress = "0.1.2.3",
Type = "TypeX"
});
});
}

在添加DataGrid时,它可以随机运行。例如,只有在点击一个元素并用键确认后才会出现新行,或者它们出现了,但只有在滚动时才可见,原来它们是";隐藏的";并且当出现新元素时,DataGrid不会展开。

我做错了什么?或者如何解决?我试着用不同的方法解决它,但它看起来总是一样的。添加新行后,DataGrid无法正确刷新。

我也没能找到一个干净的解决方案。我向DataGrid添加了VerticleAlignment="Stretch"HorizontalAlignment="Stretch"属性,这有助于直到DataGrid填满为止。如果你超过了父行的高度,那么滚动框的行为又会很奇怪。有时不会滚动到底部或一起消失。

其他不成功的尝试是在我的DataGrid项目列表更新时订阅事件,然后在DataGrid或其Parent组件上调用InvalidateVisual()InvalidateMeasure()InvalidateArrange(),但没有任何内容会刷新DataGrid。

最新更新