WPF DataGrid Column Becoming Corrupt when using Mu

2019-03-06 11:28发布

问题:

All, I am updating a StatusBar based on the number of rows selected in a DataGrid. I am doing this using MVVM. The relevant XAML is as follows

<DataGrid Grid.Row="1" AlternatingRowBackground="Gainsboro" AlternationCount="2" 
          HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
          AutoGenerateColumns="False" RowHeaderWidth="0" IsReadOnly="True"
          CanUserAddRows="False" CanUserDeleteRows="False" SelectionMode="Extended" 
          EnableRowVirtualization="False" ItemsSource="{Binding Cultures}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Code" Binding="{Binding Code}" IsReadOnly="True"/>
        <DataGridTextColumn Header="Language" Binding="{Binding Language}" IsReadOnly="True"/>
        <DataGridTextColumn Header="LocalName" Binding="{Binding LocalName}" IsReadOnly="True"/>
    </DataGrid.Columns>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

with the StatusBar defined by

<StatusBar Grid.Row="1" Margin="0,0.4,0.4,-0.4">
    <StatusBarItem DockPanel.Dock="Left" Background="#FF007ACC" Margin="0,2,0,0">
        <TextBlock Text="{Binding TotalSelectedCultures}"  Margin="5,0,0,0" Foreground="White"/>
    </StatusBarItem>
</StatusBar>

I am including this code so that you can see the options I am setting on the DataGrid; as I say, the functionaility [upon row selection with the mouse the number of rows selected is updated in the StatusBar's TextBlock] is working. For further information on the basic mechanism I am using to do this see https://stackoverflow.com/a/2615487/626442.

Now, as I select many items, I am noticing that that the first column [called 'Code'] is not displaying/rendering correctly (as shown, half of it is cropped off).

I have set EnableRowVirtualization="False" otherwise it just plane does not work, but why now is my first column not rendering correctly and how do I make it render correctly?

Thanks for your time.


Note. I have attempted to set EnableColumnVirtualization="False" but this seems worse!? I have also set both EnableColumnVirtualization="True" and EnableRowVirtualization="True" and this greatly improves performance and removes the rendering problem. However, this breaks the StatusBar updates, it just stops as soon as the view scrolls.

回答1:

take a look at http://grokys.blogspot.fr/2012/02/mvvm-and-multiple-selection-part-iv.html this should solve your prob, and make your code "cleaner"