WPF:模板中的动态操作

发布于 2024-09-10 17:37:47 字数 2251 浏览 2 评论 0原文

我已经为进度条定义了一个控制模板,使其看起来像温度计...现在我希望它在达到某个值时改变其颜色(例如..当进度条的值为 70 时,其颜色应该改变为黄色)

当前 PART_Indicator 的颜色绑定到进度条的背景颜色..背景颜色在 ValueChanged 事件处理程序中更改,因此指示器的颜色也会更改...是否有可能在模板中执行此操作只是,所以我不需要使用 ValueChanged 事件处理程序?

    <ControlTemplate x:Key="myThermometer" TargetType="{x:Type ProgressBar}">
    <ControlTemplate.Resources>
        <RadialGradientBrush x:Key="brushBowl" GradientOrigin="0.5 0.5">
            <GradientStop Offset="0" Color="Pink" />
            <GradientStop Offset="1" Color="Red" />
        </RadialGradientBrush>
    </ControlTemplate.Resources>
    <Canvas>
        <Path Name="PART_Track" Stroke="Black" StrokeThickness="5" Grid.Column="0">
            <Path.Data>
                <CombinedGeometry GeometryCombineMode="Union">
                    <CombinedGeometry.Geometry1>
                        <RectangleGeometry Rect="10,40,130,20" RadiusX="5" RadiusY="5"/>
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <EllipseGeometry Center="10,50" RadiusX="20" RadiusY="20"/>
                    </CombinedGeometry.Geometry2>
                </CombinedGeometry>
            </Path.Data>
        </Path>

        <Path Fill="{TemplateBinding Background}">
            <Path.Data>
                <EllipseGeometry Center="10,50" RadiusX="17" RadiusY="17"/>
            </Path.Data>
        </Path>

        <Path Name="PART_Indicator" Fill="{TemplateBinding Background}" Grid.Column="0">
            <Path.Data>
                <RectangleGeometry Rect="22,43,115,15" RadiusX="5" RadiusY="5"/>
            </Path.Data>
        </Path>

        <Canvas Canvas.Top="35" Canvas.Right="375">
            <Canvas.RenderTransform>
                <RotateTransform CenterX="120" CenterY="120" Angle="-270" />
            </Canvas.RenderTransform>
            <TextBlock FontWeight="Bold" FontSize="16" Foreground="Black" Text="{TemplateBinding Tag}"/>
        </Canvas>
    </Canvas>       
</ControlTemplate>

I've defined a control Template for a progressbar to make it look like a thermometer ... now i want it to change its color when reaching a certain value (for example .. when the progressbar has the value 70, its color should change to yellow)

Currently the color of PART_Indicator is bound to the background color of the progressbar .. the backgroundcolor is changed in the ValueChanged Eventhandler, and so the color of the indicator changes too... is there a possibility to do this within the template only, so i dont need to use the ValueChanged Eventhandler?

    <ControlTemplate x:Key="myThermometer" TargetType="{x:Type ProgressBar}">
    <ControlTemplate.Resources>
        <RadialGradientBrush x:Key="brushBowl" GradientOrigin="0.5 0.5">
            <GradientStop Offset="0" Color="Pink" />
            <GradientStop Offset="1" Color="Red" />
        </RadialGradientBrush>
    </ControlTemplate.Resources>
    <Canvas>
        <Path Name="PART_Track" Stroke="Black" StrokeThickness="5" Grid.Column="0">
            <Path.Data>
                <CombinedGeometry GeometryCombineMode="Union">
                    <CombinedGeometry.Geometry1>
                        <RectangleGeometry Rect="10,40,130,20" RadiusX="5" RadiusY="5"/>
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <EllipseGeometry Center="10,50" RadiusX="20" RadiusY="20"/>
                    </CombinedGeometry.Geometry2>
                </CombinedGeometry>
            </Path.Data>
        </Path>

        <Path Fill="{TemplateBinding Background}">
            <Path.Data>
                <EllipseGeometry Center="10,50" RadiusX="17" RadiusY="17"/>
            </Path.Data>
        </Path>

        <Path Name="PART_Indicator" Fill="{TemplateBinding Background}" Grid.Column="0">
            <Path.Data>
                <RectangleGeometry Rect="22,43,115,15" RadiusX="5" RadiusY="5"/>
            </Path.Data>
        </Path>

        <Canvas Canvas.Top="35" Canvas.Right="375">
            <Canvas.RenderTransform>
                <RotateTransform CenterX="120" CenterY="120" Angle="-270" />
            </Canvas.RenderTransform>
            <TextBlock FontWeight="Bold" FontSize="16" Foreground="Black" Text="{TemplateBinding Tag}"/>
        </Canvas>
    </Canvas>       
</ControlTemplate>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过潦 2024-09-17 17:37:47
<ProgressBar x:Name="progressBar" Background="{Binding RelativeSource={RelativeSource Self},Path=Value,Converter={StaticResource IntToBrushConverter}}" />


public class IntToBrushConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double val = (double)value;
        if (val > 50)
            return Brushes.Blue;
        else
            return Brushes.Green;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new Exception("Not Implemented");
    }

    #endregion
}
<ProgressBar x:Name="progressBar" Background="{Binding RelativeSource={RelativeSource Self},Path=Value,Converter={StaticResource IntToBrushConverter}}" />


public class IntToBrushConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double val = (double)value;
        if (val > 50)
            return Brushes.Blue;
        else
            return Brushes.Green;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new Exception("Not Implemented");
    }

    #endregion
}
冰雪之触 2024-09-17 17:37:47

或多或少 - 我会编写一个自定义 ValueConverter< /a> 将进度值转换为颜色并使用此转换器绑定进度条的填充。

XAML:

Fill="{TemplateBinding Progress, 
       Converter={StaticResource ProgressToColorConverter}}"

代码:

[ValueConversion(typeof(int), typeof(Color))]
public class ProgressToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int progress = (int)value;
        if (progress < 60)
            return Color.Green;
        else
            return Color.Red;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

此代码只是我的想法,尚未经过测试,但它应该显示原理。

more or less - I'd write a custom ValueConverter that converts your progress value to a Color and bind the fill of the progress bar using this converter.

XAML:

Fill="{TemplateBinding Progress, 
       Converter={StaticResource ProgressToColorConverter}}"

Code:

[ValueConversion(typeof(int), typeof(Color))]
public class ProgressToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int progress = (int)value;
        if (progress < 60)
            return Color.Green;
        else
            return Color.Red;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

This code is just from the top of my head and hasn't been tested, but it should show the principles.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文