2009-12-09 7 views
7

Sto utilizzando la barra di avanzamento di WPF e imposto il valore fino al valore massimo. Ma, una volta raggiunto, l'animazione (che è l'effetto verde) continua.Barra di avanzamento WPF continua animazione

Come posso fermarlo e avere una barra verde piena, senza animazione?

per esempio, prendere questo:

<Window x:Class="WpfApplication1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded"> 
    <Grid> 
     <ProgressBar Height="30" Name="progressBar1" VerticalAlignment="Top" Minimum="0" Maximum="100" /> 
    </Grid> 
</Window> 

e:

public partial class Window1 : Window 
{ 
    private double _min; 
    private double _max; 

    public Window1() 
    { 
     InitializeComponent(); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     _min = progressBar1.Minimum; 
     _max = progressBar1.Maximum; 
     Thread thread = new Thread(Start); 
     thread.Start(); 
    } 

    private void Start() 
    { 
     for (double i = _min; i <= _max; i++) 
     { 
      Thread.Sleep(50); 
      double value = i; 
      Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => progressBar1.Value = value)); 
     } 
    } 
} 

Quando il filo è terminato, posso sempre vedere l'animazione sulla barra di avanzamento (effetto bianco sulla barra verde)

Grazie in anticipo per il vostro aiuto

risposta

2

non sono del tutto sicuro di quello che yo vuoi dire, ma forse quello di cui hai bisogno è impostare la proprietà IsIndeterminate di ProgressBar su false. Se ciò non funziona, ti preghiamo di darci un codice per aiutarti ulteriormente!

MODIFICA: L'animazione fa parte dello stile Windows Aero e ad alcuni designer è stato pagato un sacco di soldi per ... err desgin it! Quindi non puoi semplicemente rimuovere l'animazione usando una proprietà. È possibile modificare l'aspetto visivo dei ProgressBar esistenti modificando il loro controltemplate. Di seguito è riportato l'intero controlme del ProgressBar di WPF. Attenzione: hai bisogno di tutte le risorse, il riferimento xmlns a Window.Themes e devi impostare un riferimento alla dll di PresentationFramwork.Aero. Ho inserito un commento in cui puoi apportare la modifica. Cambia il colore da FF000000 a 00000000 sul secondo e terzo gradiente e il chiarore bianco scompare. Ho provato a implementare un trigger che mostrerà l'animazione all'inizio, ma lo interromperà quando Valore == Massimo, ma non sono riuscito. Qualcun altro?

<Window x:Class="BlandProgressBarSpike.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" 
    Title="Window1" Height="300" Width="300" 
    Loaded="Window_Loaded"> 
    <Window.Resources> 
     <LinearGradientBrush x:Key="ProgressBarBackground" EndPoint="1,0" StartPoint="0,0"> 
      <GradientStop Color="#BABABA" Offset="0"/> 
      <GradientStop Color="#C7C7C7" Offset="0.5"/> 
      <GradientStop Color="#BABABA" Offset="1"/> 
     </LinearGradientBrush> 
     <LinearGradientBrush x:Key="ProgressBarBorderBrush" EndPoint="0,1" StartPoint="0,0"> 
      <GradientStop Color="#B2B2B2" Offset="0"/> 
      <GradientStop Color="#8C8C8C" Offset="1"/> 
     </LinearGradientBrush> 
     <LinearGradientBrush x:Key="ProgressBarGlassyHighlight" EndPoint="0,1" StartPoint="0,0"> 
      <GradientStop Color="#50FFFFFF" Offset="0.5385"/> 
      <GradientStop Color="#00FFFFFF" Offset="0.5385"/> 
     </LinearGradientBrush> 
     <LinearGradientBrush x:Key="ProgressBarTopHighlight" EndPoint="0,1" StartPoint="0,0"> 
      <GradientStop Color="#80FFFFFF" Offset="0.05"/> 
      <GradientStop Color="#00FFFFFF" Offset="0.25"/> 
     </LinearGradientBrush> 
     <!-- This produces the whitish,moving glow--> 
     <LinearGradientBrush x:Key="ProgressBarIndicatorAnimatedFill" EndPoint="0,0" StartPoint="-100,0" MappingMode="Absolute"> 
      <GradientStop Color="#00000000" Offset="0"/> 
      <GradientStop Color="#FF000000" Offset="0.4"/> 
      <GradientStop Color="#FF000000" Offset="0.6"/> 
      <GradientStop Color="#00000000" Offset="1"/> 
     </LinearGradientBrush> 
     <LinearGradientBrush x:Key="ProgressBarIndicatorDarkEdgeLeft" EndPoint="1,0" StartPoint="0,0"> 
      <GradientStop Color="#0C000000" Offset="0"/> 
      <GradientStop Color="#20000000" Offset="0.3"/> 
      <GradientStop Color="#00000000" Offset="1"/> 
     </LinearGradientBrush> 
     <LinearGradientBrush x:Key="ProgressBarIndicatorDarkEdgeRight" EndPoint="1,0" StartPoint="0,0"> 
      <GradientStop Color="#00000000" Offset="0"/> 
      <GradientStop Color="#20000000" Offset="0.7"/> 
      <GradientStop Color="#0C000000" Offset="1"/> 
     </LinearGradientBrush> 
     <RadialGradientBrush x:Key="ProgressBarIndicatorLightingEffectLeft" RelativeTransform="1,0,0,1,0.5,0.5" RadiusX="1" RadiusY="1"> 
      <GradientStop Color="#60FFFFC4" Offset="0"/> 
      <GradientStop Color="#00FFFFC4" Offset="1"/> 
     </RadialGradientBrush> 
     <LinearGradientBrush x:Key="ProgressBarIndicatorLightingEffect" EndPoint="0,0" StartPoint="0,1"> 
      <GradientStop Color="#60FFFFC4" Offset="0"/> 
      <GradientStop Color="#00FFFFC4" Offset="1"/> 
     </LinearGradientBrush> 
     <RadialGradientBrush x:Key="ProgressBarIndicatorLightingEffectRight" RelativeTransform="1,0,0,1,-0.5,0.5" RadiusX="1" RadiusY="1"> 
      <GradientStop Color="#60FFFFC4" Offset="0"/> 
      <GradientStop Color="#00FFFFC4" Offset="1"/> 
     </RadialGradientBrush> 
     <LinearGradientBrush x:Key="ProgressBarIndicatorGlassyHighlight" EndPoint="0,1" StartPoint="0,0"> 
      <GradientStop Color="#90FFFFFF" Offset="0.5385"/> 
      <GradientStop Color="#00FFFFFF" Offset="0.5385"/> 
     </LinearGradientBrush> 
     <Style x:Key="BlandStyle" TargetType="{x:Type ProgressBar}"> 
      <Setter Property="Foreground" Value="#01D328"/> 
      <Setter Property="Background" Value="{StaticResource ProgressBarBackground}"/> 
      <Setter Property="BorderBrush" Value="{StaticResource ProgressBarBorderBrush}"/> 
      <Setter Property="BorderThickness" Value="1"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type ProgressBar}"> 
         <Grid SnapsToDevicePixels="true" x:Name="Background"> 
          <Rectangle Fill="{TemplateBinding Background}" RadiusX="2" RadiusY="2"/> 
          <Border Margin="1" Background="{StaticResource ProgressBarGlassyHighlight}" CornerRadius="2"/> 
          <Border Margin="1" Background="{StaticResource ProgressBarTopHighlight}" BorderBrush="#80FFFFFF" BorderThickness="1,0,1,1"/> 
          <Rectangle Margin="1" x:Name="PART_Track"/> 
          <Decorator HorizontalAlignment="Left" Margin="1" x:Name="PART_Indicator"> 
           <Grid x:Name="Foreground"> 
            <Grid.RowDefinitions> 
             <RowDefinition/> 
             <RowDefinition/> 
            </Grid.RowDefinitions> 
            <Grid.ColumnDefinitions> 
             <ColumnDefinition MaxWidth="15"/> 
             <ColumnDefinition Width="0.1*"/> 
             <ColumnDefinition MaxWidth="15"/> 
            </Grid.ColumnDefinitions> 
            <Rectangle x:Name="Indicator" Fill="{TemplateBinding Foreground}" Grid.ColumnSpan="3" Grid.RowSpan="2"/> 
            <Rectangle x:Name="Animation" Fill="{TemplateBinding Foreground}" Grid.ColumnSpan="3" Grid.RowSpan="2"> 
             <Rectangle.OpacityMask> 
              <MultiBinding> 
               <MultiBinding.Converter> 
                <Microsoft_Windows_Themes:ProgressBarHighlightConverter/> 
               </MultiBinding.Converter> 
               <Binding Source="{StaticResource ProgressBarIndicatorAnimatedFill}"/> 
               <Binding Path="ActualWidth" ElementName="Background"/> 
               <Binding Path="ActualHeight" ElementName="Background"/> 
              </MultiBinding> 
             </Rectangle.OpacityMask> 
            </Rectangle> 
            <Rectangle Margin="1,1,0,1" x:Name="LeftDark" Fill="{StaticResource ProgressBarIndicatorDarkEdgeLeft}" RadiusX="1" RadiusY="1" Grid.RowSpan="2"/> 
            <Rectangle Margin="0,1,1,1" x:Name="RightDark" Fill="{StaticResource ProgressBarIndicatorDarkEdgeRight}" RadiusX="1" RadiusY="1" Grid.Column="2" Grid.RowSpan="2"/> 
            <Rectangle x:Name="LeftLight" Fill="{StaticResource ProgressBarIndicatorLightingEffectLeft}" Grid.Column="0" Grid.Row="2"/> 
            <Rectangle x:Name="CenterLight" Fill="{StaticResource ProgressBarIndicatorLightingEffect}" Grid.Column="1" Grid.Row="2"/> 
            <Rectangle x:Name="RightLight" Fill="{StaticResource ProgressBarIndicatorLightingEffectRight}" Grid.Column="2" Grid.Row="2"/> 
            <Border x:Name="Highlight1" Grid.ColumnSpan="3" Grid.RowSpan="2" Background="{StaticResource ProgressBarIndicatorGlassyHighlight}"/> 
            <Border x:Name="Highlight2" Grid.ColumnSpan="3" Grid.RowSpan="2" Background="{StaticResource ProgressBarTopHighlight}"/> 
           </Grid> 
          </Decorator> 
          <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2"/> 
         </Grid> 
         <ControlTemplate.Triggers> 
          <Trigger Property="Orientation" Value="Vertical"> 
           <Setter Property="LayoutTransform" TargetName="Background"> 
            <Setter.Value> 
             <RotateTransform Angle="-90"/> 
            </Setter.Value> 
           </Setter> 
           <Setter Property="LayoutTransform" TargetName="PART_Track"> 
            <Setter.Value> 
             <RotateTransform Angle="90"/> 
            </Setter.Value> 
           </Setter> 
           <Setter Property="LayoutTransform" TargetName="PART_Indicator"> 
            <Setter.Value> 
             <RotateTransform Angle="90"/> 
            </Setter.Value> 
           </Setter> 
           <Setter Property="LayoutTransform" TargetName="Foreground"> 
            <Setter.Value> 
             <RotateTransform Angle="-90"/> 
            </Setter.Value> 
           </Setter> 
          </Trigger> 
          <Trigger Property="IsIndeterminate" Value="true"> 
           <Setter Property="Visibility" TargetName="LeftDark" Value="Collapsed"/> 
           <Setter Property="Visibility" TargetName="RightDark" Value="Collapsed"/> 
           <Setter Property="Visibility" TargetName="LeftLight" Value="Collapsed"/> 
           <Setter Property="Visibility" TargetName="CenterLight" Value="Collapsed"/> 
           <Setter Property="Visibility" TargetName="RightLight" Value="Collapsed"/> 
           <Setter Property="Visibility" TargetName="Indicator" Value="Collapsed"/> 
          </Trigger> 
          <Trigger Property="IsIndeterminate" Value="false"> 
           <Setter Property="Fill" TargetName="Animation" Value="#80B5FFA9"/> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Window.Resources> 
    <StackPanel> 
     <ProgressBar Style="{StaticResource BlandStyle}" Value="{Binding Progress}" Height="30" 
        Minimum="0" Maximum="100"/> 
    </StackPanel>   
</Window> 
+0

IsIndeterminate impostata su false non risolvere il mio problema Ho modificato con un esempio di codice – Tim

+0

questa è una buona idea ... ho usato un trucco, un colpo della barra, come bitmap, e sostituirlo con immagine Al termine. Uggly, ma funzionante :) – Tim

+0

Finché la barra di avanzamento non viene ridimensionata, questa è una soluzione eccellente! – Dabblernl

0

penso che è una funzionalità di Windows Aero ... tutte le barre di avanzamento hanno questo effetto, non penso che può essere fermato ...

1

convenuto che l'animazione "bagliore" è fonte di confusione ad alcuni utenti. Può essere rimosso senza distruggere la funzionalità IsIndeterminate che Dabblernl: s risponde sopra.

Utilizzando lo stile Aero che si può trovare in Dabblernl: s post ho sostituito:

<Rectangle x:Name="Animation" Fill="{TemplateBinding Foreground}" Grid.ColumnSpan="3" Grid.RowSpan="2"> 
    <Rectangle.OpacityMask> 
    <MultiBinding> 
     <MultiBinding.Converter> 
     <Microsoft_Windows_Themes:ProgressBarHighlightConverter/> 
     </MultiBinding.Converter> 
     <Binding Source="{StaticResource ProgressBarIndicatorAnimatedFill}"/> 
     <Binding Path="ActualWidth" ElementName="Background"/> 
     <Binding Path="ActualHeight" ElementName="Background"/> 
    </MultiBinding> 
    </Rectangle.OpacityMask> 
</Rectangle> 

con:

<Rectangle x:Name="Animation" Grid.ColumnSpan="3" Fill="{TemplateBinding Foreground}" Grid.RowSpan="2"> 
    <Rectangle.OpacityMask> 
    <MultiBinding> 
     <MultiBinding.Converter> 
     <converters:ProgressBarHighlightOverrideConverter/> 
     </MultiBinding.Converter> 
     <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Value"/> 
     <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Maximum"/> 
     <Binding Source="{StaticResource ProgressBarIndicatorAnimatedFill}"/> 
     <Binding ElementName="Background" Path="ActualWidth"/> 
     <Binding ElementName="Background" Path="ActualHeight"/> 
    </MultiBinding> 
    </Rectangle.OpacityMask> 
</Rectangle> 

... e ha aggiunto un nuovo MultiConverter:

using System; 
using System.Globalization; 
using System.Windows; 
using System.Windows.Data; 
using Microsoft.Windows.Themes; 

namespace [Your namespace here] 
{ 
    public class ProgressBarHighlightOverrideConverter : IMultiValueConverter 
    { 
    private readonly ProgressBarHighlightConverter converter = new ProgressBarHighlightConverter(); 

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (values[0] == null || values[0] == DependencyProperty.UnsetValue || 
      values[1] == null || values[1] == DependencyProperty.UnsetValue) 
     { 
     return null; 
     } 

     var value = (Double)values[0]; 
     var maximum = (Double)values[1]; 

     return value >= maximum ? null : converter.Convert(new [] {values[2], values[3], values[4]}, targetType, parameter, culture); 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     return null; 
    } 
    } 
} 

Quindi quello che ho effettivamente fatto è sostituire Microsoft: s ProgressBarHighlightConverter con uno dei miei che ricade su il convertitore originale se e solo se la barra di avanzamento: il valore s è inferiore a quello del suo massimo.