[UWP][C#] Font size in UI controls - datagrid and listbox

Paula Morgan 276 Reputation points
2019-12-03T13:08:05.27+00:00

How can I change the font size of the header column of the datagrid? Microsoft.Toolkit.Uwp.UI.Controls.DataGrid

I also find that I cannot change the font size of the contents of a listbox either. I need to do both in code...not xaml.

Thanks,
Paula

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,851 Reputation points
    2019-12-03T14:38:04.103+00:00

    How can I change the font size of the header column of the datagrid?

    The header of DataGrid is DataGridColumnHeader that under Microsoft.Toolkit.Uwp.UI.Controls.Primitives name space. you could edit the default style like the following:

    alt text

    Edit in code behind

    For example, if i want to modify the first column header's fontsize.

    var colum = MyDataGrid.Columns[0] as DataGridBoundColumn;  
    var headerstyle = new Style(typeof(DataGridColumnHeader));  
    headerstyle.Setters.Add(new Setter(DataGridColumnHeader.FontSizeProperty, 12));    
    colum.HeaderStyle = headerstyle;  
    

    I also find that I cannot change the font size of the contents of a listbox either.

    For ListBox content, we suggest you use binding that bind the FontSize with a value that comes from your data model or code behind.

    For code behind

    public int MyFontSize { get; set; }  
      
    public MainPage()  
    {  
        this.InitializeComponent();  
        this.DataContext = this;  
        MyFontSize = 25;  
    }  
    

    Xaml (and RootPage is page name)

    alt text


1 additional answer

Sort by: Most helpful
  1. Paula Morgan 276 Reputation points
    2019-12-05T23:17:06.517+00:00

    I was able to solve my problem by creating the xaml in code and setting it to the listbox's itemtemplate: ![alt text][1] Then for the listbox: DataTemplate datatemplate = (DataTemplate)XamlReader.Load(GetDataTemplate().ToString()); ItemTemplate = datatemplate; [1]: /api/attachments/667-code.png?platform=QnA

    0 comments No comments