我对ProgressBar的XAML代码有疑问。我有以下XAML代码来显示Bar:
<ProgressBar Minimum="0" Maximum="100" Value="80" Style="{StaticResource Progress}" Grid.Row="1" Grid.Column="0" />
显示栏的控件是:
<!-- Progress bar control -->
<Style TargetType="ProgressBar" x:Key="Progress">
<Setter Property="Background" Value="{StaticResource colorProgressbackground}" />
<Setter Property="Foreground" Value="{StaticResource colorProgressactivity}" />
<Setter Property="BorderBrush" Value="{StaticResource colorProgressbackground}" />
<Setter Property="Height" Value="12" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ProgressBar}">
<Grid Name="TemplateRoot"
SnapsToDevicePixels="true">
<Rectangle Fill="{TemplateBinding Background}"
RadiusX="4"
RadiusY="4" />
<Border Background="{StaticResource colorProgressbackground}"
Margin="1"
CornerRadius="2" />
<Decorator Name="PART_Indicator" HorizontalAlignment="Left">
<Grid Name="Foreground">
<Grid Name="Animation" ClipToBounds="True">
<Border Name="PART_GlowRect" Width="{TemplateBinding Value}" BorderThickness="0" CornerRadius="4" Margin="0,0,0,0" HorizontalAlignment="Left" Background="{StaticResource colorProgressactivity}"/>
</Grid>
</Grid>
</Decorator>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
现在,问题是条形宽度为100%,但活动条(ProgressBar代码中的值变量)不是80%,而是80像素(左右)。我不能给格条带宽硬编码。如何确保活动条(PART_indicator)以百分比正确显示?
提前谢谢!
答案 0 :(得分:0)
查看控件的代码,看起来它处理设置&#39; Part_Indicator&#39;宽度自动。
来自反射器
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (this._track != null)
{
this._track.SizeChanged -= new SizeChangedEventHandler(this.OnTrackSizeChanged);
}
this._track = base.GetTemplateChild("PART_Track") as FrameworkElement;
this._indicator = base.GetTemplateChild("PART_Indicator") as FrameworkElement;
this._glow = base.GetTemplateChild("PART_GlowRect") as FrameworkElement;
if (this._track != null)
{
this._track.SizeChanged += new SizeChangedEventHandler(this.OnTrackSizeChanged);
}
if (this.IsIndeterminate)
{
this.SetProgressBarGlowElementBrush();
}
}
...
private void SetProgressBarIndicatorLength()
{
if ((this._track != null) && (this._indicator != null))
{
double minimum = base.Minimum;
double maximum = base.Maximum;
double num3 = base.Value;
double num4 = (this.IsIndeterminate || (maximum <= minimum)) ? 1.0 : ((num3 - minimum) / (maximum - minimum));
this._indicator.Width = num4 * this._track.ActualWidth;
this.UpdateAnimation();
}
}
This custom template似乎不必考虑%,您确定Part_Indicator
没有正确调整大小,但是您正在调整Part_GlowRect
错误的基础关于价值?