C# 自定义ProgressBar控件模板

C# 自定义ProgressBar控件模板,c#,wpf,progress-bar,controltemplate,C#,Wpf,Progress Bar,Controltemplate,我在使用扩展进度控制的通用模板时遇到了一些困难。基本上,模板由网格、一些文本信息和实际进度条组成 它工作得很好,除了我想切换到垂直方向。一切似乎都正确旋转,但我没有得到任何进展指标。我希望我忽略了一些愚蠢的事情,只需要第二双眼睛就能看到 以下是模板: <ControlTemplate TargetType="{x:Type local:MyProgressControl}"> <Grid x:Name="gridLayout" Background=

我在使用扩展进度控制的通用模板时遇到了一些困难。基本上,模板由网格、一些文本信息和实际进度条组成

它工作得很好,除了我想切换到垂直方向。一切似乎都正确旋转,但我没有得到任何进展指标。我希望我忽略了一些愚蠢的事情,只需要第二双眼睛就能看到

以下是模板:

<ControlTemplate TargetType="{x:Type local:MyProgressControl}">
    <Grid x:Name="gridLayout"
          Background="{TemplateBinding Background}"
          HorizontalAlignment="Stretch"
          VerticalAlignment="Stretch">

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" MinHeight="20" />
        </Grid.RowDefinitions>

        <StackPanel x:Name="stackLabels"
                    Grid.Row="0"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    Orientation="Horizontal">
            <TextBlock x:Name="txtProgress"
                       Style="{Binding TextBlockStyle, 
                                       RelativeSource={RelativeSource TemplatedParent}}"
                       Margin="3,1"
                       Text="{Binding IndicationText,
                                      RelativeSource={RelativeSource TemplatedParent}}"
                       Visibility="{Binding ShowIndicationText,
                                            RelativeSource={RelativeSource TemplatedParent},
                                            Converter={StaticResource booleanToVisibility}}" />
            <TextBlock x:Name="txtValue"
                       Style="{Binding TextBlockStyle, RelativeSource={RelativeSource TemplatedParent}}"
                       Margin="3,1"
                       Text="{Binding RoundedValue, RelativeSource={RelativeSource TemplatedParent}}"
                       Visibility="{Binding ShowProgressText,
                                    RelativeSource={RelativeSource TemplatedParent},
                                    Converter={StaticResource booleanToVisibility}}" />

            <TextBlock x:Name="txtOf"
                       Style="{Binding TextBlockStyle, RelativeSource={RelativeSource TemplatedParent}}"
                       Margin="3,1"
                       Text="/"
                       Visibility="{Binding ShowProgressText,
                                            RelativeSource={RelativeSource TemplatedParent},
                                            Converter={StaticResource booleanToVisibility}}" />

            <TextBlock x:Name="txtMaximum"
                       Style="{Binding TextBlockStyle, RelativeSource={RelativeSource TemplatedParent}}"
                       Margin="3,1"
                       Text="{Binding RoundedMaximum,
                                      RelativeSource={RelativeSource TemplatedParent}}"
                       Visibility="{Binding ShowProgressText,
                                            RelativeSource={RelativeSource TemplatedParent},
                                            Converter={StaticResource booleanToVisibility}}" />

        </StackPanel>

        <Border x:Name="PART_Track"
                Grid.Row="1"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                CornerRadius="8">
            <Border.Background>
                <LinearGradientBrush x:Name="trackBrush" StartPoint="0.5, 0" EndPoint="0.5, 1">
                    <GradientStop Offset="0.0" Color="#6A6A6A" />
                    <GradientStop Offset="0.2" Color="#949494" />
                    <GradientStop Offset="0.35" Color="#A9A9A9"  />
                    <GradientStop Offset="0.55" Color="#D3D3D3" />
                    <GradientStop Offset="0.65" Color="#949494" />
                    <GradientStop Offset="1.0" Color="#3F3F3F" />
                </LinearGradientBrush>
            </Border.Background>
            <Decorator x:Name="PART_Indicator"
                       Margin="1"
                       HorizontalAlignment="Left"
                       VerticalAlignment="Stretch">
                <Border x:Name="borderIndicator"
                        Background="{TemplateBinding BackgroundBrush}"   
                        CornerRadius="{Binding ElementName=PART_Track, Path=CornerRadius}" />
            </Decorator>
        </Border>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="Orientation" Value="Vertical">
            <Setter Property="LayoutTransform" TargetName="gridLayout">
                <Setter.Value>
                    <RotateTransform Angle="90" />
                </Setter.Value>
            </Setter>
            <Setter Property="LayoutTransform" TargetName="PART_Track">
                <Setter.Value>
                    <RotateTransform Angle="-90" />
                </Setter.Value>
            </Setter>                
            <Setter Property="HorizontalAlignment" TargetName="PART_Indicator" Value="Stretch" />
            <Setter Property="VerticalAlignment" TargetName="PART_Indicator" Value="Bottom" />
            <Setter Property="LayoutTransform" TargetName="PART_Indicator">
                <Setter.Value>
                    <RotateTransform Angle="-90" />
                </Setter.Value>
            </Setter>
            <Setter Property="HorizontalAlignment" TargetName="borderIndicator" Value="Stretch" />
            <Setter Property="VerticalAlignment" TargetName="borderIndicator" Value="Bottom" />
            <Setter Property="LayoutTransform" TargetName="borderIndicator">
                <Setter.Value>
                    <RotateTransform Angle="-90" />
                </Setter.Value>
            </Setter>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

谢谢,
wTs

我想不出你想要达到什么样的结果。使用四个嵌套的旋转级别似乎很奇怪。例如,您的borderIndicator将倒置,因为它将受到所有四种布局转换的影响:gridLayout将使其旋转+90,PART_轨迹、PART_指示器和borderIndicator将使其旋转-90。因此,总旋转将为+90-90-90-90=-180。这是你想要的吗

另一件意想不到的事情是在装饰器中使用边框的方式。显然,您的代码隐藏是为了以某种方式影响PART_轨迹和PART_指示器,但不清楚您对装饰器所做的是什么,它阻止装饰器实际成为边框,并执行当前位于其中的边框的工作

话虽如此,我认为问题出在这里:

<Setter Property="VerticalAlignment" TargetName="borderIndicator" Value="Bottom" /> 

由于边界没有自然大小,“底部”的垂直对齐将导致其高度为零


我肯定会建议你找到一种方法来减少你申请的裁员人数。事实上,在我看来,实际上只有外部两个是必要的。我也会考虑将装饰者和边框合并。

谢谢-这使我走上了正确的道路。我想“投赞成票”,但我还不能,在潜行了这么久终于成为会员之后……虽然你还不能“投赞成票”,但你可以标记我的答案为正确,因为你是提问者。啊,谢谢。我要买这种新型的互联网产品。:)我有一个类似的问题,与一个类似的,但更容易的模板。它只是在垂直方向上不起作用。那么你的解决方案是什么?