This is a migrated thread and some comments may be shown as answers.

cells fontfamily

3 Answers 67 Views
GridView
This is a migrated thread and some comments may be shown as answers.
smr
Top achievements
Rank 1
smr asked on 14 Dec 2019, 09:34 AM

hi

how can i change cells fontfamily and fontsize?

in UI and code behind

3 Answers, 1 is accepted

Sort by
0
smr
Top achievements
Rank 1
answered on 14 Dec 2019, 09:52 AM
i use telerik 2019 noxaml
0
smr
Top achievements
Rank 1
answered on 16 Dec 2019, 09:06 AM

Hello

Is used this code but i have two problem with this

code-behind:
grid_view.CurrentCellInfo = new GridViewCellInfo(grid_view.Items[i], grid_view.Columns[k]);
grid_view.CurrentCell.Style = (Style)FindResource("cell_style_red");

 

in App.xaml:
<Style x:Key="cell_style_red" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}">
    <Setter Property="FontFamily" Value="{StaticResource Segoe_UI}" />
    <Setter Property="FontSize" Value="16" />
    <Setter Property="Foreground" Value="Red" />
</Style>


and my problems is:
1. i cant use cells in down of scroll (in page i see 10 row and i cant change cells in row 11 & 12 &...)
2. if i scroll table down, all cells style come back to column style

 

0
Accepted
Dilyan Traykov
Telerik team
answered on 18 Dec 2019, 01:28 PM

Hello,

Due to RadGridView's UI virtualization mechanism, it is not recommended to work directly with the visual elements (such as the GridViewCell) and set their properties (such as Style) from code-behind.

Instead, I can recommend using the CellStyleSelector property of the columns and add your custom style if a certain condition of the underlying data item is met.

For example, you can define a MarkedProperties property in your items' class and use this to apply the conditional styling.

public ObservableCollection<string> MarkedProperties { get; set; } = new ObservableCollection<string>();

You can then define the style selector as follows:

    public class CustomCellStyleSelector : StyleSelector
    {
        public Style RedCellStyle { get; set; }

        public override Style SelectStyle(object item, DependencyObject container)
        {
            var player = item as Player;
            var cell = container as GridViewCell;
            var property = cell.DataColumn.DataMemberBinding.Path.Path;

            if (player.MarkedProperties.Contains(property))
            {
                return this.RedCellStyle;
            }

            return base.SelectStyle(item, container);
        }
    }
    <Window.Resources>
        <my:MyViewModel x:Key="MyViewModel"/>
        <my:CustomCellStyleSelector x:Key="CustomCellStyleSelector">
            <my:CustomCellStyleSelector.RedCellStyle>
                <Style TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}">
                    <Setter Property="FontFamily" Value="Segoe_UI" />
                    <Setter Property="FontSize" Value="16" />
                    <Setter Property="Foreground" Value="Red" />
                </Style>
            </my:CustomCellStyleSelector.RedCellStyle>
        </my:CustomCellStyleSelector>
    </Window.Resources>

Lastly, you need to set the CellStyleSelector for all columns in your RadGridView which will support this styling:

        public MainWindow()
        {
            InitializeComponent();
            foreach (var column in this.playersGrid.Columns)
            {
                column.CellStyleSelector = this.Resources["CustomCellStyleSelector"] as StyleSelector;
            }
        }

You can then set the marked cells like so:

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            var item = this.playersGrid.Items[31] as Player;
            var column = this.playersGrid.Columns[3] as GridViewDataColumn;
            var property = column.DataMemberBinding.Path.Path;
            item.MarkedProperties.Add(property);
        }

Of course, instead of the hardcoded numbers (31 and 3), you can iterate over a number of cells as you've done in the code snippet you provided. Please note, however, that in order for the control to update the cell's styling, you need to notify it about the change in the property via the INotifyPropertyChanged interface.

        private void MarkedProperties_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            foreach (string property in e.NewItems)
            {
                this.OnPropertyChanged(property);
            }
        }
For your convenience, I prepared a small sample project to demonstrate this approach.

Please have a look and let me know if something similar would be applicable at your end.

Regards,
Dilyan Traykov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
smr
Top achievements
Rank 1
Answers by
smr
Top achievements
Rank 1
Dilyan Traykov
Telerik team
Share this question
or