0

So ia have this ListView with Custom Column Progress-Bar:

            <ListView Name="lvFiles">        
                <ListView.Resources>
                    <DataTemplate x:Key="MyDataTemplate">
                        <Grid Margin="0,0,0,0">
                            <ProgressBar 
                                Name="progressBarColumn"
                                Maximum="100"
                                Value="{Binding Progress, UpdateSourceTrigger=PropertyChanged}" 
                                Width="{Binding Path=Width, ElementName=ProgressCell}" 
                                Margin="0,0,0,0"
                                Style="{StaticResource CustomProgressBar}" />
                            <TextBlock
                                Text="{Binding Path=Value, ElementName=progressBarColumn, StringFormat={}{0:N1}%}"
                                VerticalAlignment="Center"
                                HorizontalAlignment="Center"
                                FontSize="11.5"
                                Foreground="White"
                                Margin="0,-2,0,0"/>
                        </Grid>
                    </DataTemplate>                        
                    <ControlTemplate x:Key="ProgressBarTemplate">
                        <Label  HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </ControlTemplate>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Text }"></Setter>
                    </Style>
                </ListView.Resources>                    
            </ListView>

Style

<Style x:Key="CustomProgressBar" TargetType="ProgressBar" >
    <Setter Property="Template" >
        <Setter.Value>
            <ControlTemplate TargetType="ProgressBar">
                <Border BorderBrush="#FF103766" BorderThickness="0" Background="#FF103766" CornerRadius="0" Padding="0">
                    <Grid x:Name="PART_Track">
                        <Rectangle x:Name="PART_Indicator" HorizontalAlignment="Left">
                            <Rectangle.Style>
                                <Style TargetType="Rectangle">
                                    <Setter Property="Fill" Value="Gray"/>
                                    <Style.Triggers>
                                        <DataTrigger Value="100" Binding="{Binding Path=Value, RelativeSource={RelativeSource AncestorType=ProgressBar}}">
                                            <Setter Property="Fill" Value="LightSeaGreen"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Rectangle.Style>
                        </Rectangle>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

So in case i want to create several Styles and change this Style from code behain, is it possible ?

user979033
  • 5,430
  • 7
  • 32
  • 50

2 Answers2

0

One line of code can do that:

  progressBarColumn.Style = (Style)Application.Current.TryFindResource("CustomProgressBar");

When you have multiple custom Progress Bar styles, you just replace "CustomProgressBar" in the code above with whatever the name of your desired progress bar style actually is.

Make sure that your Styles are in a dictionary (or location) that is visible at the application level.

Stewbob
  • 16,759
  • 9
  • 63
  • 107
0

If you use the DynamicResource markup extension:

Style="{DynamicResource CustomProgressBar}" />

...you could easily switch the resource at runtime:

lvFiles.Resources["CustomProgressBar"] = Application.Current.Resources["yourOtherStyle"] as Style;

What's the difference between StaticResource and DynamicResource in WPF?

mm8
  • 163,881
  • 10
  • 57
  • 88