我会提前为长篇大论道歉。我有两个DataTables(Case和Jobs),每个都有一个DataView,我将GUI绑定到(Cases数据视图是Grid的DataContext,而Jobs数据视图是ListView的ItemsSource和TabControl的DataContext)。我第一次使用DataAdapter.Fill填充表格时,数据显示正确的案例和作业。第二次加载数据时,我调用DataTable.Clear然后调用DataAdapter.Fill,但只有作业数据显示在GUI中。案例数据无处可见,即使单步执行我可以告诉DataTable中有一行有正确的数据。也就是说,DataTable.Clear和DataAdapter.Fill正常工作;我的GUI中的网格控件只显示数据。我已经为下面的Grid控件复制了XAML。谁能帮我?!感谢。
更新:如果我第二次在表格中有多行,数据会在文本框中正确显示!到底是怎么回事?
<Grid Height="165" Width="390" DataContext="{Binding caseTableView}" >
<Grid.Resources>
<local:CaseStatusItemsSource x:Key="StatusesSource" />
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="10" />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition Height="10" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120" />
<ColumnDefinition Width="120" />
<ColumnDefinition Width="150" />
</Grid.ColumnDefinitions>
<!--Case Details-->
<Label Style="{StaticResource LabelStyle}" Grid.Row="1" Grid.Column="1" >Case Number:</Label>
<TextBox Grid.Row="1" Grid.Column="2" Style="{StaticResource TextBoxStyle}" IsReadOnly="{Binding isROCaseNumber}" LostFocus="caseNumber_LostFocus" >
<TextBox.Text>
<Binding Path="/CASENUMBER" UpdateSourceTrigger="LostFocus">
<Binding.ValidationRules>
<local:CaseNumberValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<Label Style="{StaticResource LabelStyle}" Grid.Row="2" Grid.Column="1" >Date Received:</Label>
<TextBox Grid.Row="2" Grid.Column="2" Style="{StaticResource TextBoxStyle}" Text="{Binding Path=/DATERECEIVED, StringFormat=d}" IsReadOnly="{Binding isRODateReceived}" />
<Label Style="{StaticResource LabelStyle}" Grid.Row="3" Grid.Column="1" >Status:</Label>
<ComboBox Grid.Row="3" Grid.Column="2" Width="140" Height="20" HorizontalAlignment="Left" FontFamily="Verdana" FontSize="9"
ItemsSource="{Binding statuses, Source={StaticResource StatusesSource}}" SelectedItem="{Binding Path=/STATUS, Mode=TwoWay}" IsReadOnly="{Binding isROCaseStatus}" >
</ComboBox>
<Label Style="{StaticResource LabelStyle}" Grid.Row="4" Grid.Column="1" >Date Ord. Received:</Label>
<TextBox Grid.Row="4" Grid.Column="2" Style="{StaticResource TextBoxStyle}" Text="{Binding Path=/DATEORDRECEIVED, StringFormat=d}" IsReadOnly="{Binding isRODateOrdReceived}" />
<Label Style="{StaticResource LabelStyle}" Grid.Row="5" Grid.Column="1" >Date Posted:</Label>
<TextBox Grid.Row="5" Grid.Column="2" Style="{StaticResource TextBoxStyle}" Text="{Binding Path=/DATEPOSTED, StringFormat=d}" IsReadOnly="{Binding isRODatePosted}" />
</Grid>
答案 0 :(得分:0)
微软的代码中必须有一个错误,因为我用下面的代码修复了它。
ICollectionView _cv = CollectionViewSource.GetDefaultView(_appModel.casesTable);
_cv.MoveCurrentToNext();
我只需要为案例表而不是作业表执行此操作,这就是为什么我预计某处会出现错误的原因。无论表中是否有一行或多行,作业表始终有效。