【问题标题】:AutoHide Progressbar with StyleTriggers使用 StyleTriggers 自动隐藏进度条
【发布时间】:2011-12-01 20:37:33
【问题描述】:

我想使用数据绑定在 WPF 中隐藏进度条。每当属性为0时,进度条应该隐藏:我尝试以下代码

(信息:我当前的数据上下文是一个包含整数属性'CurrentIndex'的类)

<ProgressBar Minimum="0" Maximum="100" Value="{Binding CurrentIndex, UpdateSourceTrigger=PropertyChanged}" Visibility="Visible">
    <ProgressBar.Style>
        <Style TargetType="{x:Type ProgressBar}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding CurrentIndex}" Value="0">
                    <Setter Property="Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ProgressBar.Style>
</ProgressBar>

这段代码有什么问题?为什么 CurrentIndex 为 0 时进度条仍然显示? (在后面的模型中,'CurrentIndex'的值默认为0,当控件加载时)

【问题讨论】:

    标签: wpf xaml data-binding triggers progress-bar


    【解决方案1】:

    DP precedence,不要在控件本身上设置Visibility(本地值>样式)。

    【讨论】:

    • 现在很有意义:事实上,如果我在样式中设置可见性,它就可以正常工作:
    【解决方案2】:

    使用可见性绑定和转换器的其他方式:

    <Grid>
    <Grid.Resources>
        <App:VisibilityConverter x:Key="VisibilityConverter" />
    </Grid.Resources>
    <ProgressBar Minimum="0" Maximum="100" Value="{Binding CurrentIndex, UpdateSourceTrigger=PropertyChanged}" 
                 Visibility="{Binding CurrentIndex, Mode=OneWay, Converter={StaticResource VisibilityConverter}}" />
    </Grid>
    

    转换器代码(VisibilityConverter.cs):

    public class VisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (int)value == 0 ? Visibility.Hidden : Visibility.Visible;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    【讨论】:

      【解决方案3】:

      您的 XAML 几乎是正确的!

      像你一样定义你的进度条:

      <ProgressBar Minimum="0" 
                   Maximum="100" 
                   Value="{Binding CurrentIndex, UpdateSourceTrigger=PropertyChanged}"
                   Name="MyAutoHidingProgressBar" />
      

      不要忘记添加Name 属性并且不要在此处设置Visibility。 它总是会覆盖您在Style 中设置的内容。

      然后在您的&lt;Window.Resources&gt; 中正常定义Style

       <Window.Resources>
          <Style TargetType="ProgressBar" x:Key="MyAutoHidingProgressBarStyle">
             <Style.Triggers>
                 <DataTrigger Binding="{Binding ElementName=MyAutoHidingProgressBar, Path=Value}" Value="0">
                      <Setter Property="Visibility" Value="Hidden"></Setter>
                 </DataTrigger>
             </Style.Triggers>
          </Style>
      </Window.Resources>
      

      这基本上是检查进度条本身的值,而不是你的绑定。

      最后一步将样式添加到进度条:

      Style="{StaticResource MyAutoHidingProgressBarStyle}"
      

      现在,如果 Value0,您的 ProgressBar 将自动隐藏。 您还可以轻松添加一个触发器以在其已满时将其隐藏。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-24
        相关资源
        最近更新 更多