WPF 制作虚线的边框

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="ContentControl" x:Key="myBorder">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Grid x:Name="g" Background="{TemplateBinding ContentControl.Background}" Width="{TemplateBinding ContentControl.Width}" Height="{TemplateBinding ContentControl.Height}">
                            <Canvas>
                                <Rectangle RadiusX="5" RadiusY="5" Width="{Binding ElementName=g, Path=ActualWidth}" Height="{Binding ElementName=g, Path=ActualHeight}" Stroke="{TemplateBinding ContentControl.BorderBrush}" StrokeDashArray="5,2,1,2" StrokeThickness="{TemplateBinding ContentControl.BorderThickness}"/>
                            </Canvas>
                            <ContentPresenter Margin="5" Content="{TemplateBinding ContentControl.Content}"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Grid HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="g">
            <Canvas>
                <Rectangle RadiusX="5" RadiusY="5" Width="{Binding ElementName=g, Path=ActualWidth}" Height="{Binding ElementName=g, Path=ActualHeight}" Stroke="Blue" StrokeDashArray="5,2,1,2" StrokeThickness="2" />
            </Canvas>
            <TextBlock Margin="10,7,10,7" Text="aaa" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Grid>
        <ContentControl Margin="10" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="2" BorderBrush="Blue" Style="{StaticResource myBorder}">
            <TextBlock Text="abc"/>
        </ContentControl>
    </StackPanel>
</Window>


知识共享许可协议
《WPF 制作虚线的边框》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
相邻依据:发表时间
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     173.99ms