当ItemsSource属性更改时,DataGrid不会重绘

时间:2012-03-26 15:58:09

标签: mvvm silverlight-4.0 datagrid

我的Navigatable Silverlight应用程序中有一个类似于以下内容的页面(删除了专有引用):

 <navigation:Page x:Class="MyNamespace.MachineView"
                 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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
                 xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" 
                 xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
                 xmlns:common="-- proprietary reference --" 
                 xmlns:res="-- proprietary reference --"
                 xmlns:sclHelpers="-- proprietary reference --" 
                 mc:Ignorable="d"
                 d:DesignWidth="1024" d:DesignHeight="768"
                 res:Strings.Assignment="Title=MachineView.Title">
    <Border Margin="35,10,35,0" VerticalAlignment="Top">
        <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition Height="25"/>
                    <RowDefinition/>
                    <RowDefinition Height="25"/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <toolkit:HeaderedContentControl d:LayoutOverrides="Width, Height, GridBox" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Margin="0">
                    <toolkit:HeaderedContentControl.Header>
                        <TextBlock res:Strings.Assignment="Text=MachineView.ReadyProjectsTitle" TextWrapping="Wrap" Style="{StaticResource HeaderTextStyle}"/>

                    </toolkit:HeaderedContentControl.Header>
                    <Grid>
                        <sdk:DataGrid Background="#7FFFFFFF" AutoGenerateColumns="False" ItemsSource="{Binding ReadyProjects}" d:LayoutOverrides="Width, Height" Margin="0" Grid.ColumnSpan="2" MinHeight="75">
                            <sdk:DataGrid.Columns>
                                <sdk:DataGridTextColumn res:Strings.Assignment="Header=MachineView.ProjectNumberLabel"
                                    Binding="{Binding ProjectNumberText, FallbackValue=[Project Number]}" 
                                    IsReadOnly="True"/>
                                <sdk:DataGridTextColumn res:Strings.Assignment="Header=MachineView.ProjectNameLabel"
                                    Binding="{Binding NameText, FallbackValue=[Project Name]}"
                                    IsReadOnly="True"/>
                                <sdk:DataGridTextColumn res:Strings.Assignment="Header=MachineView.ProjectStatusLabel"
                                    Binding="{Binding StatusText, FallbackValue=[Status]}"
                                    IsReadOnly="True"/>
                            </sdk:DataGrid.Columns>
                        </sdk:DataGrid>
                        <common:MiniBusyControl Height="20" HorizontalAlignment="Center" Grid.ColumnSpan="3" Margin="0,24,0,0" Visibility="{Binding ReadyProjects.IsBusy, Converter={StaticResource VisibilityConverter}}"/>
                        <TextBlock res:Strings.Assignment="Text=EmptyText" HorizontalAlignment="Center" TextWrapping="Wrap" Visibility="{Binding ReadyProjects.ShowEmpty, Converter={StaticResource VisibilityConverter}}" VerticalAlignment="Center" Margin="0,24,0,0" FontStyle="Italic"/>
                    </Grid>
                </toolkit:HeaderedContentControl>
            </Grid>
        </ScrollViewer>
    </Border>
</navigation:Page>

最初填充绑定到DataGrid属性的集合时,不会重绘ItemSource。该集合是一种直接从ObservableCollection<T>继承的类型,以便使用集合包装其他属性(即IsBusyShowEmpty)。每当我求助或调整DataGrid UI列时,数据都会显示出来,所以它肯定是一个重绘问题。好像控件本身就有一个bug。我正在使用MVVM解耦模型,但我非常接近于破解此页面背后的代码,直接在DataGrid上调用Measure。我测试了后端,它运行正常;发送适当的收集和财产更改通知。这确实非常令人沮丧。

编辑

我发现如果我直接使用ObservableCollection<T>,那一切都有效。我认为Silverlight DataGridObservableCollection<T>类型有一些硬编码依赖。有没有人对这个假设有任何见解?说它不是这样!

2 个答案:

答案 0 :(得分:0)

视图模型的示例可能有所帮助。我猜测ReadyProjects中的可观察集合为INotifyPropertyChangedIsBusy实现了ShowEmpty。您的DataContext是否为ReadyProjects属性本身更改了实例和fire属性,以注册初始集?

答案 1 :(得分:0)

好吧,我有时候是个白痴。我的问题是这样的:我重载了ObservableCollection<T>的{​​{1}}方法,但从未实际调用基类的版本。

叹息。

我希望这有助于其他人。