Wpf 创建自定义ProgressBar时出现绑定问题
I get错误:无法在“Point”类型的“X”属性上设置“Binding”。只能对DependencyObject的DependencyProperty设置“绑定” 有没有干净的解决办法?Wpf 创建自定义ProgressBar时出现绑定问题,wpf,xaml,Wpf,Xaml,I get错误:无法在“Point”类型的“X”属性上设置“Binding”。只能对DependencyObject的DependencyProperty设置“绑定” 有没有干净的解决办法? 由于Point.X不是依赖项属性,因此无法将其绑定到某个对象。不过,您可以绑定EndPointProperty,并使用转换器为您创建点。例如,它可以将Y值作为参数 Xaml 注意:可能与您的问题无关,但如果您也需要绑定Y,则可以使用如下的多重绑定 public class PointXConverter :
由于Point.X不是依赖项属性,因此无法将其绑定到某个对象。不过,您可以绑定EndPointProperty,并使用转换器为您创建点。例如,它可以将Y值作为参数 Xaml 注意:可能与您的问题无关,但如果您也需要绑定Y,则可以使用如下的多重绑定
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();
}
}
<LinearGradientBrush.EndPoint>
<Binding RelativeSource="{RelativeSource AncestorType={x:Type ProgressBar}}"
Path="Value"
Converter="{StaticResource PointXConverter}"
ConverterParameter="0"/>
</LinearGradientBrush.EndPoint>
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();
}
}
<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>
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();
}
}