通过UserControl实例说明依赖属性和绑定

看一个简单例子

https://www.cnblogs.com/dotnet261010/p/6286475.html

先绘制原始circularprogressbar

<UserControl x:Class="WpfUITest1.Controls.CircularProgressBar"
             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" 
             xmlns:local="clr-namespace:WpfUITest1.Controls"
             mc:Ignorable="d" 
             d:DesignHeight="150" d:DesignWidth="150">
    <Grid Height="{Binding RelativeSource={RelativeSource Self},Path=Width}" Name="layout">
        <Ellipse Width="{Binding ElementName=layout,Path=Width}"
                 Height="{Binding ElementName=layout,Path=Width}"
                 StrokeThickness="6" Stroke="#EEE" >
            <Ellipse.Effect>
                <DropShadowEffect ShadowDepth="0" Direction="0" Color="White" BlurRadius="5"/>
            </Ellipse.Effect>
        </Ellipse>
        <Path Name="path" Data="M75 3A75 75 0 0 1 147 75" StrokeThickness="4" Stroke="Orange"
              StrokeStartLineCap="Round" StrokeEndLineCap="Round"/>
        <!--M75 3A75 75 0 0 1 147 75-->
        <Viewbox Margin="20" >
            <TextBlock Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center">
                <Run Text="25"></Run>
                <Run Text="%"></Run>
            </TextBlock>
            <!--{Binding Value,RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor}}-->
        </Viewbox>
    </Grid>
</UserControl>
View Code

相关文章: