2017-07-06 50 views
0

所以IA有这个ListView使用自定义ColumnProgress-Bar设置的DataTemplate控件的样式代码背后

  <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 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> 

所以,如果我想创建几个Styles和代码behain改变这种Style , 可能吗 ?代码

回答

0

一号线能做到这一点:

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

当你有多个自定义进度栏样式,您只需更换“CustomProgressBar”在上面的代码与任何您想要的进度条样式的名称实际上是。

确保您的样式位于应用程序级别可见的字典(或位置)中。

+0

我无法从代码到progressBarColumn ,我的样式位于应用程序根目录下的Styles文件夹内,并且我添加了这个App.xaml: user979033

+0

我需要把这个放在哪里? – user979033

+0

用什么取代? – user979033

0

如果使用DynamicResource标记扩展:

Style="{DynamicResource CustomProgressBar}" /> 

...你可以很容易地在运行时切换资源

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

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

+0

你试过这个还是发生了什么?请接受你的问题的答案。 – mm8

相关问题