Microsoft Toolkit DataGrid无法刷新视图中的数据

时间:2019-07-18 09:12:33

标签: c# uwp datagrid toolkit

我有datagrid在UWP应用程序的xaml中显示一些数据。我使用MVVM,并且具有用于视图的视图模型。 datagrid的项源是可观察的集合。我在xaml视图上刷新数据时遇到问题。如果我使用DataGridTextColumn作为列数据,则在视图上正确刷新数据,但是如果我在视图上使用DataGridTemplateColumn数据则不会刷新。

此代码正常工作:

<mtc:DataGrid Name="ArticlesListView"
  ItemsSource="{x:Bind ViewModel.Articles, Mode=OneWay}"
  AutoGenerateColumns="False"
  HorizontalAlignment="Stretch"
  VerticalAlignment="Stretch"
  VerticalScrollBarVisibility="Auto"
  HorizontalScrollBarVisibility="Auto"
  SelectionMode="Single"
  RowDetailsVisibilityMode="Collapsed"
  IsReadOnly="True">
  <mtc:DataGrid.Columns>
     <mtc:DataGridTextColumn Header="Barcode" Binding="{Binding Barcode}"/>
     <mtc:DataGridTextColumn Header="Name" Binding="{Binding Description}"/>
  </mtc:DataGrid.Columns>
 </mtc:DataGrid>

我清除并用新数据填充了可观察的集合,并且视图上的数据已正确刷新。

此代码无法正常工作:

<mtc:DataGrid Name="ArticlesListView"
    ItemsSource="{x:Bind ViewModel.Articles, Mode=OneWay}"
    AutoGenerateColumns="False"
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    VerticalScrollBarVisibility="Auto"
    HorizontalScrollBarVisibility="Auto"
    SelectionMode="Single"
    RowDetailsVisibilityMode="Collapsed"
    IsReadOnly="True">
    <mtc:DataGrid.Columns>
       <mtc:DataGridTemplateColumn Header="Barcode">
          <mtc:DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                   <controls:DataGridCell Background="{Binding Active, Converter={StaticResource BoolToColorConverter}, ConverterParameter=true}" 
                                                   Data="{Binding Barcode}"/>
               </DataTemplate>
            </mtc:DataGridTemplateColumn.CellTemplate>
         </mtc:DataGridTemplateColumn>
       <mtc:DataGridTemplateColumn Header="Name">
         <mtc:DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
              <controls:DataGridCell Background="{Binding Active, Converter={StaticResource BoolToColorConverter}, ConverterParameter=true}" 
                                                   Data="{Binding Description}"/>
           </DataTemplate>
         </mtc:DataGridTemplateColumn.CellTemplate>
        </mtc:DataGridTemplateColumn>                
     </mtc:DataGrid.Columns>
  </mtc:DataGrid>

我清除并用新数据填充可观察的集合,但在视图上保留旧数据。

有人可以帮助我解决这个问题吗?

感谢您的帮助。我解决了这个问题。问题在于属性更改事件未达到我为DataTemplate所做的自定义控件,并且更改不适用于该控件。我更改以下代码行:

<mtc:DataGridTemplateColumn Header="Barcode" Tag="Barcode">
  <mtc:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <controls:DataGridCell Background="{Binding Active, Converter={StaticResource BoolToColorConverter}, ConverterParameter=true}" 
                             Data="{Binding Barcode}"/>
      </DataTemplate>
   </mtc:DataGridTemplateColumn.CellTemplate>
</mtc:DataGridTemplateColumn>

对此:

<mtc:DataGridTemplateColumn Header="Barcode" Tag="Barcode">
  <mtc:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>     
      <Grid Background="{Binding Active, Converter={StaticResource BoolToColorConverter}, ConverterParameter=true}">
        <TextBlock Text="{Binding Barcode, Converter={StaticResource ObjectToStringConverter}}" 
                   Style="{StaticResource DataGridCellContentStyle}"/>
      </Grid>
    </DataTemplate>
  </mtc:DataGridTemplateColumn.CellTemplate>
</mtc:DataGridTemplateColumn>

,一切正常。只需在datagrid中定义DataTemplate即可,而不必为DataTemplate使用外部用户控件,因为更改不会到达用户控件,并且不适用于视图。

0 个答案:

没有答案