WPF usercontrol circlesector按钮

时间:2016-06-12 12:10:40

标签: wpf user-controls

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

现在我刚刚尝试连接到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>

2 个答案:

答案 0 :(得分:0)

你可以添加一个&#34;属性改变触发器&#34;并将其附加到Size Changed事件,然后将对象设置为horizo​​ntal \ vertical alighment属性。

here's an example

答案 1 :(得分: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);
        }
    }

此代码段来自其他组件。