首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF视觉状态

WPF视觉状态
EN

Stack Overflow用户
提问于 2014-05-10 17:12:51
回答 1查看 952关注 0票数 1

下面是XAML的一个片段。CAn任何人都解释为什么即使VisualState Name=”Determinate”不包括任何故事板,元素(在本例中)也会改变其正常的视觉外观?

如果删除该行('VisualState Name=“确定”/‘),则ProgressBar将恢复到其原始外观。这是因为如果它不处于不可扩展状态,那么它必须处于确定性状态,这是默认功能支持的状态吗?

我真正想说的是,“确定”(VisualState Name=)的存在--尽管评论认为它什么也不做--确实会产生影响。如果我们把视觉状态线和悬停在右边的饼图上评论掉,就能看到这种影响。在这种情况下,饼图不会恢复到“正常”的外观。为什么会这样呢?会发生什么呢?

代码语言:javascript
复制
<Application x:Class="WindowsApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WindowsApplication1"
StartupUri="Window1.xaml">
    <Application.Resources>
        <LinearGradientBrush x:Key="foregroundBrush" StartPoint="0,0" EndPoint="1,1">
            <GradientStop Offset="0" Color="LightGreen"/>
            <GradientStop Offset="1" Color="DarkGreen"/>
        </LinearGradientBrush>

        <ControlTemplate x:Key="progressPie" TargetType="{x:Type ProgressBar}">
            <!-- Resources -->
            <ControlTemplate.Resources>
                <local:ValueMinMaxToPointConverter x:Key="converter1"/>
                <local:ValueMinMaxToIsLargeArcConverter x:Key="converter2"/>
            </ControlTemplate.Resources>
            <!-- Visual Tree -->
            <Viewbox>
                <!-- Visual State Groups -->
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup Name="CommonStates">
                        <VisualState Name="Determinate"/>
                        <!-- Nothing to do for this state -->
                        <VisualState Name="Indeterminate">
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="pie" Storyboard.TargetProperty="Opacity" To="0" Duration="0"/>
                                <DoubleAnimation Storyboard.TargetName="backgroundNormal" Storyboard.TargetProperty="Opacity" To="0" Duration="0"/>
                                <DoubleAnimation Storyboard.TargetName="backgroundIndeterminate" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/>
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
                <Grid Width="20" Height="20">
                    <Ellipse x:Name="backgroundIndeterminate" Opacity="0" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}" Width="20" Height="20">
                        <Ellipse.Fill>
                            <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                                <GradientStop Offset="0" Color="Yellow"/>
                                <GradientStop Offset="1" Color="Brown"/>
                            </LinearGradientBrush>
                        </Ellipse.Fill>
                    </Ellipse>
                    <Ellipse x:Name="backgroundNormal" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}" Width="20" Height="20" Fill="{TemplateBinding Background}"/>
                    <Path x:Name="pie" Fill="{TemplateBinding Foreground}">
                        <Path.Data>
                            <PathGeometry>
                                <PathFigure StartPoint="10,10" IsClosed="True">
                                    <LineSegment Point="10,0"/>
                                    <ArcSegment Size="10,10" SweepDirection="Clockwise">
                                        <ArcSegment.Point>
                                            <MultiBinding Converter="{StaticResource converter1}">
                                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Value"/>
                                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Minimum"/>
                                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Maximum"/>
                                            </MultiBinding>
                                        </ArcSegment.Point>
                                        <ArcSegment.IsLargeArc>
                                            <MultiBinding Converter="{StaticResource converter2}">
                                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Value"/>
                                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Minimum"/>
                                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Maximum"/>
                                            </MultiBinding>
                                        </ArcSegment.IsLargeArc>
                                    </ArcSegment>
                                </PathFigure>
                            </PathGeometry>
                        </Path.Data>
                    </Path>
                </Grid>
            </Viewbox>
            <!-- Only one Trigger -->
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter TargetName="pie" Property="Fill">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                                <GradientStop Offset="0" Color="Gray"/>
                                <GradientStop Offset="1" Color="White"/>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Application.Resources>
</Application>


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WindowsApplication1.Window1">
  <StackPanel Orientation="Horizontal">
    <ProgressBar Foreground="{StaticResource foregroundBrush}" Width="100"
      Height="100" Value="0" Template="{StaticResource progressPie}" Margin="10"/>
    <ProgressBar Foreground="{StaticResource foregroundBrush}" Width="100"
      Height="100" Value="10" Template="{StaticResource progressPie}" Margin="10"/>
    <ProgressBar Foreground="{StaticResource foregroundBrush}" Width="100"
      Height="100" Value="50" Template="{StaticResource progressPie}" Margin="10"/>
    <ProgressBar Foreground="{StaticResource foregroundBrush}" Width="100"
      Height="100" Value="75" Template="{StaticResource progressPie}" Margin="10"/>
    <ProgressBar Foreground="{StaticResource foregroundBrush}" Width="100"
      Height="100" Value="100" Template="{StaticResource progressPie}" Margin="10"/>
    <ProgressBar Foreground="{StaticResource foregroundBrush}" Width="100"
      Height="100" Value="10" IsEnabled="False" Template="{StaticResource progressPie}" Margin="10"/>
    <ProgressBar Foreground="{StaticResource foregroundBrush}" Width="100" 
      Height="100" Value="10" IsIndeterminate="True" Template="{StaticResource progressPie}" Margin="10" MouseEnter="RoutedEventHandler"/>
  </StackPanel>
</Window>

namespace WindowsApplication1
{
    public class ValueMinMaxToIsLargeArcConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            CultureInfo culture)
        {
            double value = (double)values[0];
            double minimum = (double)values[1];
            double maximum = (double)values[2];

            // Only return true if the value is 50% of the range or greater
            return ((value * 2) >= (maximum - minimum));
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }



       public class ValueMinMaxToPointConverter : IMultiValueConverter
        {
            public object Convert(object[] values, Type targetType, object parameter,
               CultureInfo culture)
            {
                double value = (double)values[0];
                double minimum = (double)values[1];
                double maximum = (double)values[2];

                // Convert the value to one between 0 and 360
                double current = (value / (maximum - minimum)) * 360;

                // Adjust the finished state so the ArcSegment gets drawn as a whole circle
                if (current == 360)
                    current = 359.999;

                // Shift by 90 degrees so 0 starts at the top of the circle
                current = current - 90;

                // Convert the angle to radians
                current = current * 0.017453292519943295;

                // Calculate the circle's point
                double x = 10 + 10 * Math.Cos(current);
                double y = 10 + 10 * Math.Sin(current);

                return new Point(x, y);
            }

            public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
                CultureInfo culture)
            {
                throw new NotSupportedException();
            }
        }
    }

    namespace WindowsApplication1
    {
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
            public void RoutedEventHandler(object sender,    RoutedEventArgs e)

            {
                int a = 0;
                ((ProgressBar)sender).IsIndeterminate = false;
            }
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2014-05-11 01:21:56

这是因为如果它不是处于不确定状态,那么它必须处于确定性状态,这是默认功能支持的状态吗?

是的,这是正确的。如果您查看控件模板的内容,您应该会发现它是在确定性状态下配置的--因此这是它的默认状态。更改为“不确定”状态会应用其故事板,然后更改回“确定性”会逆转故事板的效果。

如果我们注释掉视觉状态线并悬停在右边的饼图上,就可以看到这种影响。在这种情况下,饼图将不会恢复到它的“正常”外观。为什么会这样呢?幕后发生了什么?

我不确定,但我的猜测是,当控件试图进入“确定性”状态时,运行时会看到不存在这种状态,并抛出一个异常(您可以在Visual中检查输出窗口以确认这一点)。无论如何,这种行为并不令人惊讶--如果要使控件正常工作,则需要在其控件模板中包含它的所有命名部件和视觉状态

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23584052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档