views:

46

answers:

1

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

+1  A: 

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>
FuleSnabel
Looks interesting but why did You use grid in template? Sole elipse wouldnt work ?One more question You used x:Type TextBox, can I use more general Type like UserControl, or maybe can I have couple of values in TargetType?
Berial
Ellipse isn't a ContentControl and thus can't hold the ScrollViewer (where the TextBox content will be placed by the TextBox Control). Note: Grid is one of the fundamental layout panels. It's not a DataGrid. You can specify a base class for TextBox as TargetType but it won't work as you expect PART_ConentHost is specially for TextBox. You can make styles which other styles can be based on though. Ohh, and never define UserControls classes. It's just a very poor pattern for WPF. It unfortunately got tranferred from WinForms to WPF. Makes me sad when thinking about it.
FuleSnabel
Thanks for explanation but I still have problems to acomplish my aims.I need to create a custon control of shape like this<Polygon Points=" 0 0, 40 0 , 60 20 , 40 40 , 0 40" Fill="Red"> </Polygon>or at least I need to insert into this polygon two textboxes.However I think it is not possible. XAML prevents me for placing any textbox inside taht polygon :( What is more I need this polygon to Fill entire space available (Polygon will be placed in Grid I suppose ). Is it possible to do sth like this??
Berial