2014-02-13 36 views
1

我正在嘗試爲ProgressBar編寫一個WPF樣式,它將標準欄變成「Progress pie」。爲WPF ProgressBar編寫「Progress Pie」風格

這是我到目前爲止已經試過:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d"> 

    <Style x:Key="ProgressPie" TargetType="{x:Type ProgressBar}"> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ProgressBar}"> 
        <Grid x:Name="TemplateRoot" SnapsToDevicePixels="true"> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="*"/> 
         </Grid.ColumnDefinitions> 

         <Ellipse x:Name="PART_Track" 
           Fill="{TemplateBinding Background}" 
           HorizontalAlignment="Stretch" 
           VerticalAlignment="Stretch"/> 

         <ed:Arc x:Name="PART_Indicator" 
           ArcThickness="1" 
           ArcThicknessUnit="Percent" 
           Fill="{StaticResource SomeStaticBrush}" 
           ToolTip="{TemplateBinding Value}" 
           EndAngle="{TemplateBinding Value}"/> 

         <ed:Arc x:Name="OuterPieBorder" 
           ArcThickness="{TemplateBinding BorderThickness}" 
           ArcThicknessUnit="Pixel" 
           Stroke="{TemplateBinding BorderBrush}" 
           StartAngle="0" 
           EndAngle="360" 
           HorizontalAlignment="Stretch" 
           VerticalAlignment="Stretch" 
           Margin="0"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ResourceDictionary> 

不幸的是我至少有幾個問題:

  • 看來的PART_Indicator寬度必然的了Value模板。怎麼來的?我沒有寫任何文件。
  • 我找不到一個簡單的方法來定位PART_Indicator,使餡餅的中心與PART_Track的中心重合;有什麼建議麼?

回答

1

PART_Indicator的寬度似乎被綁定到模板的值。怎麼來的?我沒有寫任何文件。

也就是說模板是如何工作的:) see this for more explanations.

關於你提到的第二個問題,我沒有看到任何「神奇的答案」(我猜心不是),但this answer might help you.

如果你能讀法文,或你相信谷歌翻譯,there is this one as well這是做你想做的,似乎很完整。

+0

謝謝,在等待答案的同時,我也發現了這一點。總結一下: * PART_Indicator的寬度被模板設置爲Value * PART_Track,顯然沒有任何關係。 *弧形不允許自然指定中心點的位置,以實現自定義形狀是必需的。 – elnigno

+0

你明白了......但我不明白爲什麼這是一個問題......你沒有義務申報PART_Indicator,是嗎? – Olivier

+0

我不確定,我試着重命名它,應用程序仍然會編譯;然而,當我嘗試顯示進度條時,我遇到了崩潰。 – elnigno