WPF自定义形状控件 [英] WPF custom shape control

查看:161
本文介绍了WPF自定义形状控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was wondering if it is possible to make a custom shape custom control. I need to make control which contains textbox, however the control must have a shape of triangle or sth more sophisticated than regular rectangle/square.

推荐答案

答案是肯定的。
长答案正在阅读WPF控件样式:

Short answer is yes. Long answer is reading up on WPF Control Styles:

<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>

这篇关于WPF自定义形状控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆