Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF MVVM
Need to create a data grid with configurable columns. In that the cell colors will change according to the data bind.
 
This can be done by data trigger of that column(xaml) - 'only the cell background color'.
 
But i need to generate the dynamic column from view model class(mvvm wpf). And need to generate data triggers for the corresponding columns.
 
I have spent lots of day on googling but the end result is null.
Posted 3-Mar-13 23:25pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi. try this way :
 
<DataGridTextColumn Header="Subject" >
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBox}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type TextBox}">
                                        <TextBlock Text="{TemplateBinding Text}"/>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding YourFavoriteProperty}" Value="">
                                    <Setter Property="Background" Value=""/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
if this doesn't work tell me.
Good Luck
  Permalink  
Comments
vinothezhilan m at 4-Mar-13 9:38am
   
actually i use AutoGenerateColumns="True". how can i add datagridtextcolumn to that datagrid
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi.this doesn't work completely. if you complete it, please tell me:
 
<Style x:Key="myStyle" TargetType="{x:Type DataGridCell}">
 
           <Setter Property="Template">
               <Setter.Value>
                   <ControlTemplate TargetType="{x:Type DataGridCell}">
                       <ContentPresenter >
                           <ContentPresenter.ContentTemplate>
                               <DataTemplate>
                                   <TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=DataGridCell, Mode=FindAncestor}, Path=Content}"/>
                               </DataTemplate>
                           </ContentPresenter.ContentTemplate>
                       </ContentPresenter>
                   </ControlTemplate>
               </Setter.Value>
           </Setter>
       </Style>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 278
1 Maciej Los 205
2 DamithSL 193
3 Nguyen.H.H.Dang 190
4 arvind mepani 187


Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 4 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid