Click here to Skip to main content
12,406,342 members (70,692 online)
Rate this:
 
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 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
Top Experts
Last 24hrsThis month


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100