垂直进度条模板.NET进度条、模板、NET

由网友(独揽帅氕)分享简介:我尝试定制的.Net垂直进度条(使用Visual 2010),具有以下code:I try to customize a .Net vertical progress bar (using Visual 2010), with the following code:我尝试定制的.Net垂直进度条(使用Visual 2010),具有以下code:

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>

但是,它doesn't显示进度,有什么不对?

But, it doesn´t show the progress, What's wrong?

推荐答案

你可能会碰到的问题是一个的重大更改的在.NET 4.0中是试图改善的结果的结果WPF的进度控制性能。 (东西,诚然,它需要很糟糕。)请参阅http://connect.microsoft.com/VisualStudio/feedback/details/571674/issue-with-vertical-progress-bar-on-4-0-framework

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 http://connect.microsoft.com/VisualStudio/feedback/details/571674/issue-with-vertical-progress-bar-on-4-0-framework

短版:在.net 3.5和更早的版本,你可以编写进度栏,它是足够聪明来调整自身的垂直或水平方向。在.NET 4.0中,你的必须的创作进度条,就好像它是水平的,则使用触发器和改造把它旋转到垂直方向时的方向是垂直的。如果你使用的.Net 4.0(我怀疑你),这就是为什么它是不可能的,你让你的进度条来表现你期望的方式。

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.

可悲的是,这是的非常的记录在MSDN不佳。这是值得注意的是,例如在 http://msdn.microsoft.com/en-us /library/ms750638.aspx 没有提到这一点,或以任何方式处理。同样,进度文档本身(的http:// MSDN。 microsoft.com/en-us/library/system.windows.controls.progressbar.aspx )不会记录从presentation基础的早期版本中这个重大更改。感谢老天爷,已提交的bug,或者我(也)会认为我疯了。

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:

<ControlTemplate TargetType="ProgressBar">

  <Border x:Name="Root">
    <!-- Visual tree goes here, be sure to include PART_Indicator and PART_Track -->
  </Border>

  <!-- Triggers must come after the Visual Tree so we can reference names. -->
  <ControlTemplate.Triggers>
    <Trigger Property="Orientation" Value="Vertical">

      <!-- Rotate the progressbar so the left edge is the bottom edge -->
      <Setter TargetName="Root" Property="LayoutTransform">
        <Setter.Value>
          <RotateTransform Angle="270" />
        </Setter.Value>
      </Setter>

      <!-- 
      Fix the render dimensions b/c what was the width is now height and vice-versa. 
      Note that we have to use {RelativeSource TemplatedParent} b/c {TemplateBinding}
      can't be used in a setter's value.
      -->
      <Setter TargetName="Root" Property="Width"
       Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Height}"
       />
      <Setter TargetName="Root" Property="Height"
       Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Width}"
       />
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>
阅读全文

相关推荐

最新文章