创建自定义ProgressBar时的绑定问题

发布于 2024-10-16 14:39:07 字数 2208 浏览 1 评论 0原文

<UserControl x:Class="WpfApplication2.ProgressBar"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <ProgressBar Minimum="0" Maximum="1"  Value="0.5" LargeChange="0.1" SmallChange="0.01" Margin="2,2,12,2" Height="22">
            <ProgressBar.Template>
                <ControlTemplate>
                    <Border BorderThickness="2" BorderBrush="Black">
                        <Rectangle>
                            <Rectangle.Fill>
                                <LinearGradientBrush StartPoint="0,0">
                                    <LinearGradientBrush.EndPoint>
                                        <Point Y="0" X="{Binding RelativeSource={RelativeSource AncestorType={x:Type ProgressBar}}, Path=ProgressBar.Value}"/>
                                    </LinearGradientBrush.EndPoint>
                                    <GradientStop Color="Transparent" Offset="1.01"/>
                                    <GradientStop Color="#FF0000" Offset="1.0"/>
                                    <GradientStop Color="#FFFF00" Offset="0.50"/>
                                    <GradientStop Color="#00FF00" Offset="0.0"/>
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>
                    </Border>
                </ControlTemplate>
            </ProgressBar.Template>
        </ProgressBar>
        <TextBlock Text="50%" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
</UserControl>

我收到错误:“无法在“Point”类型的“X”属性上设置“Binding”。只能在 DependencyObject 的 DependencyProperty 上设置“Binding”。”

  • 有什么干净的解决方法吗?
<UserControl x:Class="WpfApplication2.ProgressBar"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <ProgressBar Minimum="0" Maximum="1"  Value="0.5" LargeChange="0.1" SmallChange="0.01" Margin="2,2,12,2" Height="22">
            <ProgressBar.Template>
                <ControlTemplate>
                    <Border BorderThickness="2" BorderBrush="Black">
                        <Rectangle>
                            <Rectangle.Fill>
                                <LinearGradientBrush StartPoint="0,0">
                                    <LinearGradientBrush.EndPoint>
                                        <Point Y="0" X="{Binding RelativeSource={RelativeSource AncestorType={x:Type ProgressBar}}, Path=ProgressBar.Value}"/>
                                    </LinearGradientBrush.EndPoint>
                                    <GradientStop Color="Transparent" Offset="1.01"/>
                                    <GradientStop Color="#FF0000" Offset="1.0"/>
                                    <GradientStop Color="#FFFF00" Offset="0.50"/>
                                    <GradientStop Color="#00FF00" Offset="0.0"/>
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>
                    </Border>
                </ControlTemplate>
            </ProgressBar.Template>
        </ProgressBar>
        <TextBlock Text="50%" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
</UserControl>

I get error: "A 'Binding' cannot be set on the 'X' property of type 'Point'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject."

  • Is there any clean workaround?

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

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

发布评论

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

评论(1

谜兔 2024-10-23 14:39:07

由于 Point.X 不是依赖属性,因此您无法将其绑定到某些东西。不过,您可以绑定 EndPointProperty,并使用转换器为您创建 Point。它可以将 Y 值作为参数,例如

Xaml

<LinearGradientBrush.EndPoint>
    <Binding RelativeSource="{RelativeSource AncestorType={x:Type ProgressBar}}"
             Path="Value"
             Converter="{StaticResource PointXConverter}"
             ConverterParameter="0"/>
</LinearGradientBrush.EndPoint>

PointXConverter

public class PointXConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double progressBarValue = (double)value;
        double yValue = System.Convert.ToDouble(parameter);
        return new Point(progressBarValue, yValue);
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

注意:可能与您的问题无关,但如果您还需要绑定 Y,您可以使用 <代码>MultiBinding就像这样

<LinearGradientBrush.EndPoint>
    <MultiBinding Converter="{StaticResource PointConverter}">
        <Binding RelativeSource="{RelativeSource AncestorType={x:Type ProgressBar}}"
                 Path="Value"/>
        <Binding RelativeSource="{RelativeSource AncestorType={x:Type ProgressBar}}"
                 Path="Value"/>
    </MultiBinding>                                        
</LinearGradientBrush.EndPoint>

PointConverter

public class PointConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double xValue = (double)values[0];
        double yValue = (double)values[1];
        return new Point(xValue, yValue);
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Since Point.X isn't a Dependency Property you can't bind it to something. You could bind the EndPointProperty though, and use a Converter that creates the Point for you. It could take the Y value as parameter for example

Xaml

<LinearGradientBrush.EndPoint>
    <Binding RelativeSource="{RelativeSource AncestorType={x:Type ProgressBar}}"
             Path="Value"
             Converter="{StaticResource PointXConverter}"
             ConverterParameter="0"/>
</LinearGradientBrush.EndPoint>

PointXConverter

public class PointXConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double progressBarValue = (double)value;
        double yValue = System.Convert.ToDouble(parameter);
        return new Point(progressBarValue, yValue);
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Note: Probably not related to your question but if you would need to bind Y as well, you can use a MultiBinding like this

<LinearGradientBrush.EndPoint>
    <MultiBinding Converter="{StaticResource PointConverter}">
        <Binding RelativeSource="{RelativeSource AncestorType={x:Type ProgressBar}}"
                 Path="Value"/>
        <Binding RelativeSource="{RelativeSource AncestorType={x:Type ProgressBar}}"
                 Path="Value"/>
    </MultiBinding>                                        
</LinearGradientBrush.EndPoint>

PointConverter

public class PointConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double xValue = (double)values[0];
        double yValue = (double)values[1];
        return new Point(xValue, yValue);
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文