Skip to main content

Microsoft Silverlight

Answered Question DataGrid: How to set different row backgound for different rows from viewModelRSS Feed

(0)

kobruleht
kobruleht

Member

Member

161 points

579 Posts

DataGrid: How to set different row backgound for different rows from viewModel

 I'm looking for a way to use viewmodel to set DataGrid RowBackGround based on this row property values.

I tried code below but viewModel RowBackGround property getter is called only once. How to force RowBackGround getter in viewModel called for every row ?

Andrus.

    <UserControl.Resources>
        <content:VirtualGridFormModel x:Key="viewModel" />
    </UserControl.Resources>

    <Grid DataContext="{StaticResource viewModel}">

            <DataGrid  RowBackground="{Binding Path=RowBackground, Mode=TwoWay}"
           />
>

public class VirtualGridFormModel: INotifyPropertyChanged {
   public Brush RowBackground
        {
            get
            {

....

Sergey.Lutay
Sergey.L...

Contributor

Contributor

7198 points

1,340 Posts

Re: DataGrid: How to set different row backgound for different rows from viewModel

Hi Andrus,

You can't do that from xaml code. Use LoadingRow event of DataGrid and set background of row in handler

(If this has answered your question, please click on "mark as answer" on this post. Thank you!)

Blog

Twitter

Sincerely,
Sergey Lutay

yifung
yifung

Contributor

Contributor

3313 points

540 Posts

Microsoft
Answered Question

Re: DataGrid: How to set different row backgound for different rows from viewModel

If you re-template the DataGridRow, you can also binding the background to your view model using a converter that returns a brush when given a value.

Yifung Lin [MSFT]
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities