2011-02-09 50 views
2
<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> 

时,我得到错误:“A‘绑定’不能在A型的'点‘绑定’的‘X’属性设置只能在DependencyObject的一个DependencyProperty设置。 “。绑定问题创建自定义进度

  • 是否有任何清理解决方法?

回答

5

由于Point.X不是一个依赖项属性,所以不能将它绑定到某些东西。你可以绑定EndPointProperty,然后使用一个Converter为你创建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(); 
    } 
}