使用多选时,WPF DataGrid列会损坏 [英] WPF DataGrid Column Becoming Corrupt when using Multi-Selection

查看:101
本文介绍了使用多选时,WPF DataGrid列会损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,我根据 DataGrid 中选择的行数更新一个 StatusBar 。我正在使用MVVM。相关的XAML如下

 < DataGrid Grid.Row =1AlternatingRowBackground =GainsboroAlternationCount =2 
Horizo​​ntalAlignment =StretchVerticalAlignment =Stretch
AutoGenerateColumns =FalseRowHeaderWidth =0IsReadOnly =True
CanUserAddRows =FalseCanUserDeleteRows =FalseSelectionMode =扩展
EnableRowVirtualization =FalseItemsSource ={Binding Cultures}>
< DataGrid.Columns>
< DataGridTextColumn Header =CodeBinding ={Binding Code}IsReadOnly =True/>
< DataGridTextColumn Header =LanguageBinding ={Binding Language}IsReadOnly =True/>
< DataGridTextColumn Header =LocalNameBinding ={Binding LocalName}IsReadOnly =True/>
< /DataGrid.Columns>
< DataGrid.RowStyle>
< Style TargetType =DataGridRow>
< Setter Property =IsSelectedValue ={Binding IsSelected,Mode = TwoWay}/>
< / Style>
< /DataGrid.RowStyle>
< / DataGrid>

定义的 StatusBar >

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

我正在包含此代码,以便您可以看到我在 DataGrid ;正如我所说,功能性[使用鼠标进行行选择时,选择的行数将在 StatusBar TextBlock ]正在工作。有关我使用的基本机制的更多信息,请参阅
这应该可以解决您的问题,并使您的代码更清洁


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 http://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.

解决方案

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"

这篇关于使用多选时,WPF DataGrid列会损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆