WPF自定义形状控制

时间:2010-08-14 12:15:23

标签: wpf custom-controls shape

我想知道是否可以制作自定义形状自定义控件。 我需要进行包含文本框的控件,但是控件必须具有三角形或比普通矩形/正方形更复杂的形状。

1 个答案:

答案 0 :(得分:2)

简短的回答是肯定的。 很长的答案是阅读WPF控件样式:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640" 
    Height="480">

    <Window.Resources>
        <Style x:Key="TextBoxSample" TargetType="{x:Type TextBox}">
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Grid>
                            <Ellipse 
                                x:Name="Border" 
                                Stroke="#FF393785" 
                                StrokeThickness="2"
                                >
                                <Ellipse.Fill>
                                    <RadialGradientBrush GradientOrigin="0.25,0.25" RadiusY="0.75" RadiusX="0.75">
                                        <GradientStop Color="White" Offset="0.2"/>
                                        <GradientStop Color="#FF2EC452" Offset="0.5"/>
                                        <GradientStop Color="#FF606060" Offset="1"/>
                                    </RadialGradientBrush>
                                </Ellipse.Fill>
                                </Ellipse>                              
                            <!-- The implementation places the Content into the ScrollViewer. It must be named PART_ContentHost for the control to function -->
                            <ScrollViewer 
                                x:Name="PART_ContentHost" 
                                Background="Transparent"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Fill" Value="#000" TargetName="Border"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>
    <Grid x:Name="LayoutRoot">
        <TextBox 
            Style="{StaticResource TextBoxSample}"
            VerticalAlignment="Center"
            HorizontalAlignment="Center" 
            Text="TextBox" 
            Width="180" 
            Height="180"
            />
    </Grid>
</Window>