Quantcast
Channel: Extended WPF Toolkit™ Community Edition
Viewing all articles
Browse latest Browse all 4964

New Post: Applying Styles to each column in Grid Dynamically

$
0
0
Hello I am using <Xcdg:Datagridcontrol> in my project. My customer requested to customize the styles(like foreground background, font family, font size, font stretch,font style) of each column in grid dynamically. I have used below logic to update the fore ground deponds on the cell value but it works for single column. If i apply the same logic for each column it is not working properly. It takes style which is applied to last column to all other columns.

This logic is only working for Foreground.Its throwing Null exception for font weight,font stretch,font style,font size when binding the values

XAML:
<xcdg:DataGridControl.Resources>                  
                    <DataTemplate x:Key="SignalTypeDataTemplate">
                        <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=xcdg:DataRow},Path=DataContext.SignalType}" Foreground="{Binding RelativeSource={RelativeSource AncestorType=xcdg:DataRow},Path=DataContext.Foreground}" FontSize="{Binding RelativeSource={RelativeSource AncestorType=xcdg:DataRow},Path=DataContext.FontSize}" FontStyle="{Binding RelativeSource={RelativeSource AncestorType=xcdg:DataRow},Path=DataContext.FontStyle}" FontFamily="{Binding RelativeSource={RelativeSource AncestorType=xcdg:DataRow},Path=DataContext.FontFamily}" FontStretch="{Binding RelativeSource={RelativeSource AncestorType=xcdg:DataRow},Path=DataContext.FontStretch}"/>
                    </DataTemplate>
                    <DataTemplate x:Key="CardnameTemplate">
                        <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=xcdg:DataRow},Path=DataContext.CardName}" Foreground="{Binding RelativeSource={RelativeSource AncestorType=xcdg:DataRow},Path=DataContext.CardForeground}" FontSize="{Binding RelativeSource={RelativeSource AncestorType=xcdg:DataRow},Path=DataContext.FontSize}" FontStyle="{Binding RelativeSource={RelativeSource AncestorType=xcdg:DataRow},Path=DataContext.FontStyle}" FontFamily="{Binding RelativeSource={RelativeSource AncestorType=xcdg:DataRow},Path=DataContext.FontFamily}" FontStretch="{Binding RelativeSource={RelativeSource AncestorType=xcdg:DataRow},Path=DataContext.FontStretch}"/>
                    </DataTemplate>               
                </xcdg:DataGridControl.Resources>
                <xcdg:DataGridControl.Columns>                    
                    <xcdg:Column Title="ProjectName" FieldName="ProjectName">                        
                    </xcdg:Column>                      
                    
                    <xcdg:Column Title="CardName"
                                 FieldName="CardName"
                                 IsMainColumn="True"  CellContentTemplate="{StaticResource CardnameTemplate}"/>
                    <xcdg:Column Title="SignalType" FieldName="SignalType" CellContentTemplate="{StaticResource SignalTypeDataTemplate}"/>
                    <xcdg:Column Title="PortNumber" FieldName="PortNumber" />
                    <xcdg:Column Title="TestcaseID" FieldName="TestcaseID"/>
                    <xcdg:Column Title="MeasurementParameter" FieldName="MeasurementParameter">
                    </xcdg:Column>
                    <xcdg:Column Title="ReferenceValue" FieldName="ReferenceValue" />
                    <xcdg:Column Title="MeasuredValue" FieldName="MeasuredValue" />
                    <xcdg:Column Title="CANMessageValue" FieldName="CANMessageValue" />
                    <xcdg:Column Title="ExpectedTolerance" FieldName="ExpectedTolerance" />
                    <xcdg:Column Title="ActualTolerance" FieldName="ActualTolerance" />
                    <xcdg:Column Title="Status" FieldName="Status" >
                    </xcdg:Column>
                </xcdg:DataGridControl.Columns>
__Code Behind:
__
 foreach (CardStyle _cardstyleelement in db.CardStyles.Where(x => x.Id == grouping.Id))
                        {
                            foreach (var value in Results)
                            {
                                if (value.CardName == _cardstyleelement.CardName)
                                {
                                    value.CardName = _cardstyleelement.CardName;
                                    value.FontFamily = new FontFamily(_cardstyleelement.FontFamily);
                                    value.FontSize = _cardstyleelement.FontSize;
                                    string[] _SplitFontStye = _cardstyleelement.FontStyle.Split('-');
                                    FontWeight fontWt = new FontWeight();
                                    switch (_SplitFontStye[1])
                                    {
                                        case "Bold":
                                            fontWt = FontWeights.Bold;
                                            break;
                                        case "ExtraBold":
                                            fontWt = FontWeights.ExtraBold;
                                            break;
                                        case "Normal":
                                            fontWt = FontWeights.Normal;
                                            break;
                                        case "Light":
                                            fontWt = FontWeights.Light;
                                            break;
                                        default:
                                            fontWt = FontWeights.Normal;
                                            break;
                                    }
                                    value.FontWeight = fontWt;
                                    FontStretch fontStretch = new FontStretch();
                                    switch (_SplitFontStye[2])
                                    {
                                        case "Bold":
                                            fontStretch = FontStretches.Condensed;
                                            break;
                                        case "ExtraBold":
                                            fontStretch = FontStretches.UltraCondensed;
                                            break;
                                        case "Normal":
                                            fontStretch = FontStretches.Normal;
                                            break;
                                        case "Light":
                                            fontStretch = FontStretches.Expanded;
                                            break;
                                        default:
                                            fontStretch = FontStretches.ExtraExpanded;
                                            break;
                                    }
                                    value.FontStretch = fontStretch;

                                    FontStyle fontstyle = new FontStyle();
                                    switch (_SplitFontStye[0])
                                    {
                                        case "Normal":
                                            fontstyle = FontStyles.Normal;
                                            break;
                                        case "Italic":
                                            fontstyle = FontStyles.Italic;
                                            break;
                                        case "Obilique":
                                            fontstyle = FontStyles.Oblique;
                                            break;

                                        default:
                                            fontStretch = FontStretches.Normal;
                                            break;
                                    }
                                    value.FontStyle = fontstyle;

                                    value.CardForeground = new BrushConverter().ConvertFromString(_cardstyleelement.FontColor) as SolidColorBrush;
                                }
                            }
                        }
Am using DatagridCollection view as item source not a Datatable.
   this.Results = new ObservableCollection<TestCaseResult>();
   collectionView = new DataGridCollectionView(this.Results);
   ResultGrid.ItemsSource = collectionView;
Please help me to resolve the same. I am struck with this nearly 2 weeks.

Viewing all articles
Browse latest Browse all 4964

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>