2010-07-21 86 views
1

我为进度条定义了一个控制模板,使它看起来像一个温度计......现在我希望它在达到某个值时更改其颜色(例如,当进度条值为70,其颜色应变为黄色)WPF:模板中的动态操作

当前,PART_Indicator的颜色与进度条的背景颜色绑定在一起。backgroundColor在ValueChanged事件句柄中更改,因此指示器的颜色也发生变化...有没有可能在模板中做到这一点,所以我不需要使用ValueChanged事件处理程序?

<ControlTemplate x:Key="myThermometer" TargetType="{x:Type ProgressBar}"> 
    <ControlTemplate.Resources> 
     <RadialGradientBrush x:Key="brushBowl" GradientOrigin="0.5 0.5"> 
      <GradientStop Offset="0" Color="Pink" /> 
      <GradientStop Offset="1" Color="Red" /> 
     </RadialGradientBrush> 
    </ControlTemplate.Resources> 
    <Canvas> 
     <Path Name="PART_Track" Stroke="Black" StrokeThickness="5" Grid.Column="0"> 
      <Path.Data> 
       <CombinedGeometry GeometryCombineMode="Union"> 
        <CombinedGeometry.Geometry1> 
         <RectangleGeometry Rect="10,40,130,20" RadiusX="5" RadiusY="5"/> 
        </CombinedGeometry.Geometry1> 
        <CombinedGeometry.Geometry2> 
         <EllipseGeometry Center="10,50" RadiusX="20" RadiusY="20"/> 
        </CombinedGeometry.Geometry2> 
       </CombinedGeometry> 
      </Path.Data> 
     </Path> 

     <Path Fill="{TemplateBinding Background}"> 
      <Path.Data> 
       <EllipseGeometry Center="10,50" RadiusX="17" RadiusY="17"/> 
      </Path.Data> 
     </Path> 

     <Path Name="PART_Indicator" Fill="{TemplateBinding Background}" Grid.Column="0"> 
      <Path.Data> 
       <RectangleGeometry Rect="22,43,115,15" RadiusX="5" RadiusY="5"/> 
      </Path.Data> 
     </Path> 

     <Canvas Canvas.Top="35" Canvas.Right="375"> 
      <Canvas.RenderTransform> 
       <RotateTransform CenterX="120" CenterY="120" Angle="-270" /> 
      </Canvas.RenderTransform> 
      <TextBlock FontWeight="Bold" FontSize="16" Foreground="Black" Text="{TemplateBinding Tag}"/> 
     </Canvas> 
    </Canvas>  
</ControlTemplate> 

回答

0
<ProgressBar x:Name="progressBar" Background="{Binding RelativeSource={RelativeSource Self},Path=Value,Converter={StaticResource IntToBrushConverter}}" /> 


public class IntToBrushConverter : IValueConverter 
{ 
    #region IValueConverter Members 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     double val = (double)value; 
     if (val > 50) 
      return Brushes.Blue; 
     else 
      return Brushes.Green; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new Exception("Not Implemented"); 
    } 

    #endregion 
} 
0

或多或少 - 我会写一个自定义ValueConverter你的进度值转换为颜色,并使用该转换器进度条的填充绑定。

XAML:

Fill="{TemplateBinding Progress, 
     Converter={StaticResource ProgressToColorConverter}}" 

代码:

[ValueConversion(typeof(int), typeof(Color))] 
public class ProgressToColorConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     int progress = (int)value; 
     if (progress < 60) 
      return Color.Green; 
     else 
      return Color.Red; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

此代码只是从我的头顶,并没有经过测试,但它应该表现出的原则。