1

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

4

1 回答 1

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>
于 2010-08-14T12:49:26.567 回答