0

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).

Display Error

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.

4
  • 1
    Strange indeed. That SO article sounds a little hacky, especially where it says "Be sure to set EnableRowVirtualization=False". I wonder if the selected items binding could be accomplished using an attached property instead? I used one to bind the r/o Columns property to a collection in my VM. See: stackoverflow.com/questions/320089/…. I wonder if you could adapt that code to work with the selected items? Jul 4, 2013 at 9:56
  • Hi Andrew, I actually asked the same question as this (as all searches came up null) here it was a great solution to binding to a variable number of columns. However, I am not sure what you mean by this suggestion; how do you feel binding to columns can help in this case, or have I miss-understood? Thanks for your time...
    – MoonKnight
    Jul 4, 2013 at 10:02
  • One of the answers in that SO link includes code for an attached property that makes it possible to bind to the Columns property (not normally possible, as it's R/O). I thought you might be able to adapt that code to create an attached property that lets you bind to your grid's SelectedItems property instead (also R/O, and therefore not normally possible to bind to). If this is feasible, it may be preferable to your current approach to binding (using each row's IsSelected property), as this could be the cause of your strange problem. Jul 4, 2013 at 13:22
  • Thanks for the idea. I will look into it and let you know the outcome...
    – MoonKnight
    Jul 4, 2013 at 13:24

1 Answer 1

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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.