Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

Hi.this doesn't work completely. if you complete it, please tell me:

XML
<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>
 
Share this answer
 
Hi. try this way :

XML
<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
 
Share this answer
 
Comments
vinothezhilan m 4-Mar-13 9:38am    
actually i use AutoGenerateColumns="True". how can i add datagridtextcolumn to that datagrid

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900