我在添加和删除项目时遇到问题ObservableCollection。单击绑定的按钮后,SQL Server数据库成功更新,但是调用方法WorkerInfoCollection.Add和WorkerInfoCollection.RemoveUI后未更新。此外,WorkerInfoCollection.Remove(SelectedWorker)完全不删除项目。仅当我使用LINQ First或Default时,我才能删除此项目,但是-UI不变。
经过几个小时的调试和网络搜索的,我试图改变按钮命令参数:ElementName和Path使用RelativeSource:AncestorType,在视图模型构造设定指令值,设置在二传手命令值,提高OnPropertyChanged中ObservableCollection,使用从绑定,添加和删除项目的不同模式ObservableCollection,甚至尝试ObservableCollection在运行时直接从数据库重新初始化并填充它(有点愚蠢)。没有任何帮助。在网络中搜索也无济于事。
MainWindow.xaml
<Grid>
<DataGrid Name="WorkerInfoData"
HorizontalAlignment="Stretch"
Margin="10,10,50,10"
VerticalAlignment="Stretch"
ItemsSource="{Binding WorkerInfoCollection}"
SelectedItem="{Binding SelectedWorker,
Mode=TwoWay}">
<DataGrid.Columns>
<DataGridTextColumn Header="ID ?????????"
Binding="{Binding WorkerId}" />
<DataGridTemplateColumn Header="????"
Width="SizeToCells"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding WorkerPhoto}"
Height="75"
Width="75"
Stretch="UniformToFill"
RenderOptions.BitmapScalingMode="Fant"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="???"
Binding="{Binding WorkerFullName}" />
<...>
<DataGridTextColumn Header="?????"
Binding="{Binding WorkerHobby}" />
</DataGrid.Columns>
</DataGrid>
<StackPanel Panel.ZIndex="3"
Name="addRecordPanel"
Orientation="Horizontal"
HorizontalAlignment="Right"
Margin="0,0,-369,0" …Run Code Online (Sandbox Code Playgroud)