垂直进度条模板.net
我尝试使用以下代码自定义 .Net 垂直进度条(使用 Visual 2010):
<ProgressBar Name="VolumeMeter" Orientation="Vertical" Margin="4,30,0,0" Value="50" HorizontalAlignment="Left" VerticalAlignment="Top" Height="300" Width="10">
<ProgressBar.Template>
<ControlTemplate TargetType="ProgressBar">
<Border BorderBrush="Green" BorderThickness="1">
<Grid Name="PART_Track" Background="Red">
<Rectangle Name="PART_Indicator" Fill="Blue"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Orientation" Value="Horizontal">
<Setter TargetName="PART_Indicator" Property="VerticalAlignment" Value="Stretch"/>
<Setter TargetName="PART_Indicator" Property="HorizontalAlignment" Value="Left"/>
</Trigger>
<Trigger Property="Orientation" Value="Vertical">
<Setter TargetName="PART_Indicator" Property="VerticalAlignment" Value="Bottom"/>
<Setter TargetName="PART_Indicator" Property="HorizontalAlignment" Value="Stretch"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ProgressBar.Template>
</ProgressBar>
但是,它不显示进度,出了什么问题?
I try to customize a .Net vertical progress bar (using Visual 2010), with the following code:
<ProgressBar Name="VolumeMeter" Orientation="Vertical" Margin="4,30,0,0" Value="50" HorizontalAlignment="Left" VerticalAlignment="Top" Height="300" Width="10">
<ProgressBar.Template>
<ControlTemplate TargetType="ProgressBar">
<Border BorderBrush="Green" BorderThickness="1">
<Grid Name="PART_Track" Background="Red">
<Rectangle Name="PART_Indicator" Fill="Blue"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Orientation" Value="Horizontal">
<Setter TargetName="PART_Indicator" Property="VerticalAlignment" Value="Stretch"/>
<Setter TargetName="PART_Indicator" Property="HorizontalAlignment" Value="Left"/>
</Trigger>
<Trigger Property="Orientation" Value="Vertical">
<Setter TargetName="PART_Indicator" Property="VerticalAlignment" Value="Bottom"/>
<Setter TargetName="PART_Indicator" Property="HorizontalAlignment" Value="Stretch"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ProgressBar.Template>
</ProgressBar>
But, it doesn´t show the progress, What's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能遇到的问题是 .Net 4.0 中的一项重大更改造成的,该更改是尝试提高 WPF ProgressBar 控件性能的结果。 (不可否认,它非常需要。)请参阅 此问题。
简短版本:在 .Net 3.5 及更早版本中,您可以编写进度条,并且它足够智能,可以自行调整垂直或水平方向。在 .Net 4.0 中,您必须将进度条设计为水平方向,然后在方向为垂直时使用触发器和转换将其旋转为垂直方向。如果您正在使用 .Net 4.0(我怀疑您正在使用),这就是为什么您不可能让进度条按照您期望的方式运行。
遗憾的是,MSDN 中对此的记录非常很少。值得注意的是 http://msdn.microsoft.com/en- 中的示例us/library/ms750638.aspx 没有提及这一点,也没有以任何方式解决它。同样,ProgressBar 文档本身 (http://msdn .microsoft.com/en-us/library/system.windows.controls.progressbar.aspx)没有记录演示基础早期版本的这一重大更改。感谢上帝提交了这个错误,否则我(也)会认为我疯了。
现在执行此操作的正确方法如下所示:
The problem you might be running into is the result of a breaking change in .Net 4.0 that is a result of attempts to improve the performance of the WPF ProgressBar control. (Something that, admittedly, it needed badly.) Refer to this issue.
Short version: In .Net 3.5 and earlier, you could author the progress bar and it was smart enough to adjust for vertical or horizontal orientation on its own. In .Net 4.0, you must author your progress bar as though it was horizontal, then use a trigger and transform to rotate it into a vertical orientation when the orientation is vertical. If you're using .Net 4.0 (which I suspect you are), this is why it's impossible for you to get your progress bar to behave the way you expect.
Sadly, this is very poorly documented in MSDN. It's notable that the example at http://msdn.microsoft.com/en-us/library/ms750638.aspx does not mention this, or address it in any way. Likewise, the ProgressBar documentation itself (http://msdn.microsoft.com/en-us/library/system.windows.controls.progressbar.aspx) does not document this breaking change from the prior versions of the presentation foundation. Thank goodness for the bug that was submitted, or I (too) would have thought I'd gone crazy.
The correct way to do this is now something like the following:
从您的 xaml 来看,您似乎没有将其绑定到任何东西。
您必须有一些东西告诉它更新进度,通常您会将其绑定到数据模型上的某些东西,或者在代码隐藏中使用某种方法来执行此操作。
from your xaml, it looks like you are not binding it to anything.
you have to have something that tells it to update the progress, normally you would bind it to something on your datamodel, or have some method in your codebehind that does it.
类似于:
Something like:
<Grid Height="{Binding CurrentValue}" VerticalAlignment="Bottom">