ModernWpf icon indicating copy to clipboard operation
ModernWpf copied to clipboard

How to extend a control element, such as button?

Open isness opened this issue 3 years ago • 2 comments

I hava a simple class that extends an oridinary button. However, in the UI the button is not styled, unlike other simple System.Windows.Controls.Button objects. How do I make it follow the same ModernWpf styling?

public partial class JobButton : Button
    {
        readonly string startJob = "Start";
        readonly string endJob = "Stop";
        public void ToggleJob()
        {
            if (MainWindow.IsJobStarted)
            {
                Content = startJob;
            }
            else
            {
                Content = endJob;
            }
        }
    }
<local:JobButton Content="Start"
                Margin="10,0,0,10"
                Click="ToggleItemEnterJob"
                Height="30"
                VerticalAlignment="Bottom"
                HorizontalAlignment="Left"
                Width="120" />

isness avatar Nov 27 '21 08:11 isness

Make your style based on overrides.

FoxTes avatar Dec 19 '21 18:12 FoxTes

I've been adding a BasedOn attribute. Not sure if this is the right way, but it seems to work:

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource DefaultButtonStyle}">
  <Setter Property="MinWidth" Value="70" />
  <Setter Property="MinHeight" Value="25" />
  <Setter Property="Margin" Value="5,0,0,0" />
</Style>

clovett avatar Feb 14 '22 04:02 clovett