wpf 如何将 ProgressBar 放在按钮中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16424328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Put ProgressBar in Button
提问by Buck
I am a newbie at WPF and did not pull off my attempt at putting a progressbar in a button. My previous question asked is at : StackOverflow Post. Any help with how to properly do this would be appreciated. I did not see another post on StackOverflow to address this. Ideally, it would be great to have the button (1) with text centered vertically (i.e. as appears normally in a button) and (2) with the progress bar below the text. The button would be 2 times default height to allow space for the progressbar. I can tweak position and spacing of anything you come up with. I am very interested in getting the bindings done correctly to update the progressbar.
我是 WPF 的新手,并没有完成我在按钮中放置进度条的尝试。我之前提出的问题是:StackOverflow Post。任何有关如何正确执行此操作的帮助将不胜感激。我没有在 StackOverflow 上看到另一篇文章来解决这个问题。理想情况下,按钮 (1) 的文本垂直居中(即通常出现在按钮中)和 (2) 文本下方的进度条会很棒。该按钮将是默认高度的 2 倍,以便为进度条留出空间。我可以调整你想出的任何东西的位置和间距。我对正确完成绑定以更新进度条非常感兴趣。
Thanks! Buck
谢谢!巴克
回答by Tejas Sharma
You have two options, either create a UserControlor a new style for your button and override the ControlTemplate. I would recommend that you style Buttonsince its easier and you don't end up creating a new type. Here's how you would do that.
您有两个选择,UserControl为您的按钮创建一个样式或一个新样式并覆盖ControlTemplate. 我建议您设计样式,Button因为它更容易,而且您最终不会创建新类型。这是你将如何做到的。
<Button>
<Button.Style>
<Style BasedOn="{StaticResource {x:Type Button}}" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<!-- Put in your textbox and progress bar and what not -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button>
You can read more about templating controls hereand can find the default Styleand ControlTemplatefor Buttonhere.
回答by PoweredByOrange
You can easily put any control in the Contentproperty of the button:
您可以轻松地将任何控件放在Content按钮的属性中:
<Button>
<Button.Content>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="5">
<TextBlock Text="I'm a button with progress bar!"></TextBlock>
<ProgressBar Height="20"></ProgressBar>
</StackPanel>
</Button.Content>
</Button>
This will result in a button control like this:
这将产生一个像这样的按钮控件:


If you need more than one, you should declare a Styleand apply that to the buttons in the form, as others suggested.
如果您需要多个,您应该声明 aStyle并将其应用于表单中的按钮,正如其他人建议的那样。
回答by Meysam Chegini
Use Mah app
使用 Mah 应用
<Button Canvas.Left="170"
Canvas.Top="71"
Width="55"
Command="{Binding SearchEmployeeCommand}">
<Canvas Width="50" Height="20">
<TextBlock Text="select ..." />
<controls:ProgressRing Canvas.Left="15"
Width="20"
Height="20"
IsActive="True"
Visibility="Visible" />
</Canvas>
</Button>
回答by Chris Persichetti
If you just need a single instance of this you could do something like this:
如果您只需要一个这样的实例,您可以执行以下操作:
<Button Click="Button_Click_1">
<StackPanel>
<TextBlock Text="Hello World"/>
<ProgressBar Height="10" x:Name="pb1" Maximum="15"></ProgressBar>
</StackPanel>
</Button>
Then in your C# code you could access the progress bar such as:
然后在您的 C# 代码中,您可以访问进度条,例如:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
pb1.Value++;
}

