Skip to main content
Fixed typo in the question.
Source Link
Andrew KeepCoding
  • 15.4k
  • 2
  • 25
  • 40

WinUi3 WinUI 3 C# Create custom Button

<customcontrol:IconButton Width="100" Height="100"
     iconCode="&#xE716;" 
     Type="AddButtonStyle"/>
<customcontrol:IconButton Width="100" Height="100"
     iconCode="&#xE716;" 
     Type="AddButtonStyle"/>

    /// <summary>
    /// Custom Control for an icon in a small button
    /// </summary>
    public sealed partial class IconButton : Button
    {
        /// <summary>
        /// icon code to add to the button
        /// </summary>
        public string IconCode { get; set; } = string.Empty;
        /// <summary>
        /// Type of the Style to apply on the Button 
        /// </summary>
        public CustomButtonType Type { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public IconButton()
        {
            this.DefaultStyleKey = typeof(IconButton);
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void OnApplyTemplate()
        {
            try
            {
                base.OnApplyTemplate();
            }catch
            {
                Console.WriteLine("error base.OnApplyTemplate");
            }
            try
            {
                switch (Type)
                {
                    case CustomButtonType.AccentButtonStyle:
                        this.Style = FournierStyles.MainStyles["AccentButtonStyle"] as Style;
                        break;
                    case CustomButtonType.AddButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["AddButtonStyle"] as Style;
                        break;
                    case CustomButtonType.PrincipalButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["PrincipalButtonStyle"] as Style;
                        break;
                    case CustomButtonType.DangerButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["DangerButtonStyle"] as Style;
                        break;
                    case CustomButtonType.SecondaryButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["SecondaryButtonStyle"] as Style;
                        break;
                    case CustomButtonType.WarningButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["WarningButtonStyle"] as Style;
                        break;
                    case CustomButtonType.InfoButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["InfoButtonStyle"] as Style;
                        break;
                    case CustomButtonType.SucessButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["SucessButtonStyle"] as Style;
                        break;
                    case CustomButtonType.DefaultButtonStyle:
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw new StyleTypeException("Impossible to set Style to button for " + ex);
            }
            try
            {
                FontIcon icon = new()
                {
                    Glyph = IconCode,
                    Foreground = new SolidColorBrush(Colors.Black)
                };
                this.Content = icon;
                this.UpdateLayout();
                this.Width = 30;
                this.Height = 30;
            }
            catch (Exception ex)
            {
                throw new IconButtonGenerationException("Impossible to creation icon to IconButton because " + ex);
            }
        }
    }

    /// <summary>
    /// Custom Control for an icon in a small button
    /// </summary>
    public sealed partial class IconButton : Button
    {
        /// <summary>
        /// icon code to add to the button
        /// </summary>
        public string IconCode { get; set; } = string.Empty;
        /// <summary>
        /// Type of the Style to apply on the Button 
        /// </summary>
        public CustomButtonType Type { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public IconButton()
        {
            this.DefaultStyleKey = typeof(IconButton);
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void OnApplyTemplate()
        {
            try
            {
                base.OnApplyTemplate();
            }catch
            {
                Console.WriteLine("error base.OnApplyTemplate");
            }
            try
            {
                switch (Type)
                {
                    case CustomButtonType.AccentButtonStyle:
                        this.Style = FournierStyles.MainStyles["AccentButtonStyle"] as Style;
                        break;
                    case CustomButtonType.AddButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["AddButtonStyle"] as Style;
                        break;
                    case CustomButtonType.PrincipalButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["PrincipalButtonStyle"] as Style;
                        break;
                    case CustomButtonType.DangerButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["DangerButtonStyle"] as Style;
                        break;
                    case CustomButtonType.SecondaryButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["SecondaryButtonStyle"] as Style;
                        break;
                    case CustomButtonType.WarningButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["WarningButtonStyle"] as Style;
                        break;
                    case CustomButtonType.InfoButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["InfoButtonStyle"] as Style;
                        break;
                    case CustomButtonType.SucessButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["SucessButtonStyle"] as Style;
                        break;
                    case CustomButtonType.DefaultButtonStyle:
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw new StyleTypeException("Impossible to set Style to button for " + ex);
            }
            try
            {
                FontIcon icon = new()
                {
                    Glyph = IconCode,
                    Foreground = new SolidColorBrush(Colors.Black)
                };
                this.Content = icon;
                this.UpdateLayout();
                this.Width = 30;
                this.Height = 30;
            }
            catch (Exception ex)
            {
                throw new IconButtonGenerationException("Impossible to creation icon to IconButton because " + ex);
            }
        }
    }

WinUi3 C# Create custom Button

<customcontrol:IconButton Width="100" Height="100"
     iconCode="&#xE716;" 
     Type="AddButtonStyle"/>

    /// <summary>
    /// Custom Control for an icon in a small button
    /// </summary>
    public sealed partial class IconButton : Button
    {
        /// <summary>
        /// icon code to add to the button
        /// </summary>
        public string IconCode { get; set; } = string.Empty;
        /// <summary>
        /// Type of the Style to apply on the Button 
        /// </summary>
        public CustomButtonType Type { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public IconButton()
        {
            this.DefaultStyleKey = typeof(IconButton);
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void OnApplyTemplate()
        {
            try
            {
                base.OnApplyTemplate();
            }catch
            {
                Console.WriteLine("error base.OnApplyTemplate");
            }
            try
            {
                switch (Type)
                {
                    case CustomButtonType.AccentButtonStyle:
                        this.Style = FournierStyles.MainStyles["AccentButtonStyle"] as Style;
                        break;
                    case CustomButtonType.AddButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["AddButtonStyle"] as Style;
                        break;
                    case CustomButtonType.PrincipalButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["PrincipalButtonStyle"] as Style;
                        break;
                    case CustomButtonType.DangerButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["DangerButtonStyle"] as Style;
                        break;
                    case CustomButtonType.SecondaryButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["SecondaryButtonStyle"] as Style;
                        break;
                    case CustomButtonType.WarningButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["WarningButtonStyle"] as Style;
                        break;
                    case CustomButtonType.InfoButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["InfoButtonStyle"] as Style;
                        break;
                    case CustomButtonType.SucessButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["SucessButtonStyle"] as Style;
                        break;
                    case CustomButtonType.DefaultButtonStyle:
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw new StyleTypeException("Impossible to set Style to button for " + ex);
            }
            try
            {
                FontIcon icon = new()
                {
                    Glyph = IconCode,
                    Foreground = new SolidColorBrush(Colors.Black)
                };
                this.Content = icon;
                this.UpdateLayout();
                this.Width = 30;
                this.Height = 30;
            }
            catch (Exception ex)
            {
                throw new IconButtonGenerationException("Impossible to creation icon to IconButton because " + ex);
            }
        }
    }

WinUI 3 C# Create custom Button

<customcontrol:IconButton Width="100" Height="100"
     iconCode="&#xE716;" 
     Type="AddButtonStyle"/>
    /// <summary>
    /// Custom Control for an icon in a small button
    /// </summary>
    public sealed partial class IconButton : Button
    {
        /// <summary>
        /// icon code to add to the button
        /// </summary>
        public string IconCode { get; set; } = string.Empty;
        /// <summary>
        /// Type of the Style to apply on the Button 
        /// </summary>
        public CustomButtonType Type { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public IconButton()
        {
            this.DefaultStyleKey = typeof(IconButton);
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void OnApplyTemplate()
        {
            try
            {
                base.OnApplyTemplate();
            }catch
            {
                Console.WriteLine("error base.OnApplyTemplate");
            }
            try
            {
                switch (Type)
                {
                    case CustomButtonType.AccentButtonStyle:
                        this.Style = FournierStyles.MainStyles["AccentButtonStyle"] as Style;
                        break;
                    case CustomButtonType.AddButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["AddButtonStyle"] as Style;
                        break;
                    case CustomButtonType.PrincipalButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["PrincipalButtonStyle"] as Style;
                        break;
                    case CustomButtonType.DangerButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["DangerButtonStyle"] as Style;
                        break;
                    case CustomButtonType.SecondaryButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["SecondaryButtonStyle"] as Style;
                        break;
                    case CustomButtonType.WarningButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["WarningButtonStyle"] as Style;
                        break;
                    case CustomButtonType.InfoButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["InfoButtonStyle"] as Style;
                        break;
                    case CustomButtonType.SucessButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["SucessButtonStyle"] as Style;
                        break;
                    case CustomButtonType.DefaultButtonStyle:
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw new StyleTypeException("Impossible to set Style to button for " + ex);
            }
            try
            {
                FontIcon icon = new()
                {
                    Glyph = IconCode,
                    Foreground = new SolidColorBrush(Colors.Black)
                };
                this.Content = icon;
                this.UpdateLayout();
                this.Width = 30;
                this.Height = 30;
            }
            catch (Exception ex)
            {
                throw new IconButtonGenerationException("Impossible to creation icon to IconButton because " + ex);
            }
        }
    }
Source Link
Riddsaw
  • 47
  • 1
  • 4

WinUi3 C# Create custom Button

I'm trying to create a custom button for a small icon An icon in a small green button but the button only show when i'm refreshing it in the xaml code or resizing window. Do you know how to make it show at the initiation of the page? Or do it in better way?

I'm using a custom control to use like

<customcontrol:IconButton Width="100" Height="100"
     iconCode="&#xE716;" 
     Type="AddButtonStyle"/>

the IconButton Class :


    /// <summary>
    /// Custom Control for an icon in a small button
    /// </summary>
    public sealed partial class IconButton : Button
    {
        /// <summary>
        /// icon code to add to the button
        /// </summary>
        public string IconCode { get; set; } = string.Empty;
        /// <summary>
        /// Type of the Style to apply on the Button 
        /// </summary>
        public CustomButtonType Type { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public IconButton()
        {
            this.DefaultStyleKey = typeof(IconButton);
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void OnApplyTemplate()
        {
            try
            {
                base.OnApplyTemplate();
            }catch
            {
                Console.WriteLine("error base.OnApplyTemplate");
            }
            try
            {
                switch (Type)
                {
                    case CustomButtonType.AccentButtonStyle:
                        this.Style = FournierStyles.MainStyles["AccentButtonStyle"] as Style;
                        break;
                    case CustomButtonType.AddButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["AddButtonStyle"] as Style;
                        break;
                    case CustomButtonType.PrincipalButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["PrincipalButtonStyle"] as Style;
                        break;
                    case CustomButtonType.DangerButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["DangerButtonStyle"] as Style;
                        break;
                    case CustomButtonType.SecondaryButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["SecondaryButtonStyle"] as Style;
                        break;
                    case CustomButtonType.WarningButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["WarningButtonStyle"] as Style;
                        break;
                    case CustomButtonType.InfoButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["InfoButtonStyle"] as Style;
                        break;
                    case CustomButtonType.SucessButtonStyle:
                        this.Style = FournierStyles.ButtonStyles["SucessButtonStyle"] as Style;
                        break;
                    case CustomButtonType.DefaultButtonStyle:
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw new StyleTypeException("Impossible to set Style to button for " + ex);
            }
            try
            {
                FontIcon icon = new()
                {
                    Glyph = IconCode,
                    Foreground = new SolidColorBrush(Colors.Black)
                };
                this.Content = icon;
                this.UpdateLayout();
                this.Width = 30;
                this.Height = 30;
            }
            catch (Exception ex)
            {
                throw new IconButtonGenerationException("Impossible to creation icon to IconButton because " + ex);
            }
        }
    }

The IconButton Class will be use to get input for CRUD modification.