2016-06-12 65 views
0

如何使绑定工作,以便在设计时我可以重新计算我需要渲染路径的三个poisitions。当有人更改自定义控件的大小?WPF usercontrol circlesector按钮

现在我只是试图连接到SizeChanged事件..

<Grid DataContext="{Binding ElementName=mainbutton}"> 
    <Path Stroke="{Binding Path=BorderBrush}" StrokeThickness="1" Fill="{Binding Path=Foreground}"> 
     <Path.Data> 
      <PathGeometry> 
       <PathGeometry.Figures> 
        <PathFigureCollection> 
         <PathFigure IsClosed="True" StartPoint="{Binding Path= CenterPoint}"> 
          <PathFigure.Segments> 
           <PathSegmentCollection> 
            <!--<Path Stroke="Black" StrokeThickness="1" Data="M 180,180 L 271.9,88.1 A130,130 0 0 0 88.1,88.1 Z"/>--> 
            <LineSegment Point="{Binding Path= TopLeftPoint}"/> 
            <ArcSegment Point="{Binding Path= TopRightPoint}" Size="{Binding Path= Size}" IsLargeArc="False" SweepDirection="Clockwise" RotationAngle="0"/> 
           </PathSegmentCollection> 
          </PathFigure.Segments> 
         </PathFigure> 
        </PathFigureCollection> 
       </PathGeometry.Figures> 
      </PathGeometry> 
     </Path.Data> 
    </Path> 
</Grid> 

回答

0

以及ü可以添加一个“属性更改触发”,并将其连接到改变大小事件,然后设定目标水平\垂直alighment属性。

here's an example

0

我找到了解决这个。

XAML文件:

 <Path Stroke="CornflowerBlue" StrokeThickness="5" StrokeStartLineCap="Round" StrokeEndLineCap="Round"> 
     <Path.Data> 
      <PathGeometry> 
       <PathFigure StartPoint="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ArcProgress}},Path=StartPoint, Mode=OneWay}"> 
        <ArcSegment Point="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ArcProgress}},Path=EndPoint, Mode=OneWay}" 
           Size="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ArcProgress}},Path=CircleSize, Mode=OneWay}" SweepDirection="Clockwise" /> 
       </PathFigure> 
      </PathGeometry> 
     </Path.Data> 
    </Path> 

并为CS文件。

public partial class ArcProgress : System.Windows.Controls.UserControl 
{ 
    public static readonly DependencyProperty ProgressProperty = 
     DependencyProperty.Register("Progress", typeof(double), typeof(ArcProgress), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender,(o, e) => { ((ArcProgress)o).UpdateGeometry(); })); 

    static ArcProgress() 
    { 
     WidthProperty.OverrideMetadata(typeof(ArcProgress), new FrameworkPropertyMetadata((o, e) => { ((ArcProgress)o).UpdateGeometry(); })); 
     HeightProperty.OverrideMetadata(typeof(ArcProgress), new FrameworkPropertyMetadata((o, e) => { ((ArcProgress)o).UpdateGeometry(); })); 

    }  

    private void UpdateGeometry() 
    { 
     if (!double.IsNaN(Width) && !double.IsNaN(Height)) 
     { 

      double x = (Progress/100.0) * Width; 
      double y = (Progress/100.0) * Height; 
      StartPoint = new Point(x, 0); 
      EndPoint = new Point(0, y); 
      CircleSize = new Size(x, y); 
     } 
    } 

这段代码来自另一个组件。