如何使用类创建WPF Rounded按钮

时间:2017-08-24 20:37:56

标签: c# wpf

我想使用class而不是XAML样式创建Rounded按钮

此代码在WinForms应用程序中工作,如何将其转换为WPF代码?

HEAD detached at ca6dfb3
Untracked files:
  (use "git add <file>..." to include in what will be committed)
    yarn.lock
nothing added to commit but untracked files present (use "git add" to track)
Dropped refs/stash@{0} (8ab81738da8330c59a8d91b0b3cef454b607dd3d)
/Users/travis/.rvm/gems/ruby-2.4.1/gems/dpl-1.8.40/lib/dpl/provider/s3.rb:47:in `chdir': No such file or directory @ dir_chdir - build/*.dmg (Errno::ENOENT)
    from /Users/travis/.rvm/gems/ruby-2.4.1/gems/dpl-1.8.40/lib/dpl/provider/s3.rb:47:in `push_app'
    from /Users/travis/.rvm/gems/ruby-2.4.1/gems/dpl-1.8.40/lib/dpl/provider.rb:154:in `block in deploy'
    from /Users/travis/.rvm/gems/ruby-2.4.1/gems/dpl-1.8.40/lib/dpl/cli.rb:41:in `fold'
    from /Users/travis/.rvm/gems/ruby-2.4.1/gems/dpl-1.8.40/lib/dpl/provider.rb:154:in `deploy'
    from /Users/travis/.rvm/gems/ruby-2.4.1/gems/dpl-1.8.40/lib/dpl/provider/s3.rb:75:in `deploy'
    from /Users/travis/.rvm/gems/ruby-2.4.1/gems/dpl-1.8.40/lib/dpl/cli.rb:32:in `run'
    from /Users/travis/.rvm/gems/ruby-2.4.1/gems/dpl-1.8.40/lib/dpl/cli.rb:7:in `run'
    from /Users/travis/.rvm/gems/ruby-2.4.1/gems/dpl-1.8.40/bin/dpl:5:in `<top (required)>'
    from /Users/travis/.rvm/gems/ruby-2.4.1/bin/dpl:23:in `load'
    from /Users/travis/.rvm/gems/ruby-2.4.1/bin/dpl:23:in `<main>'
failed to deploy
/Users/travis/.travis/job_stages: line 878: shell_session_update: command not found

1 个答案:

答案 0 :(得分:2)

假设你有一个非常这样做的好理由(比如你真的想在更复杂的场景中绘制图表或类似的自定义绘图),你可以这样做:

public class RoundButton : Button
{
    public RoundButton()
    {
        DefaultStyleKey = typeof(RoundButton);
    }

    protected override void OnRender(DrawingContext dc)
    {
        double radius = 10;
        double borderThickness = 1; // Could get this value from any of the this.BorderThickness values

        dc.DrawRoundedRectangle(Background, new Pen(BorderBrush, borderThickness), new Rect(0, 0, Width, Height), radius, radius);
    }
}

但我真的,真的建议在这种情况下转而使用XAML路线。自定义绘图在这里没有任何意义。

上述代码的一个明显问题是,为了使其工作,您必须禁用默认按钮样式,否则将在绘图顶部绘制一个按钮。

在这种情况下,RoundButton的样式不存在,并且控件没有为文本或其他内容的位置定义占位符。如果你想要这个,你最好用控件模板定义这个样式,并且可以在那里放置视觉效果。