Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating not rectangle control for input

Writing an app for Windows Phone, I want to create custom control, inherited from TextBlock. But form of this control should be not rectangular. I tried to use Blend for this task, but I couldn't find properties to change form of controls.

desirable form of custom control

On the image above there is schematic form of control. I suppose, that there is possibility to set coordinates of angles of control, but I didn't find it. Thank you.

like image 554
Max Zhukov Avatar asked Aug 14 '26 07:08

Max Zhukov


1 Answers

This problem can be solved in several ways, I chose to use a Template with their figures. In the role of the figures will be performing standard Rectangles. Template for TextBlock can not be set, so I opted for a more universal control - Label.

Example:

<Style TargetType="{x:Type Label}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Label}">
                <Border Background="{TemplateBinding Background}">
                    <Grid>
                        <Rectangle Width="30" Height="70" Fill="Gainsboro" StrokeThickness="1" Margin="0,0,0,10" Panel.ZIndex="0" />
                        <ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="33,0,0,25" Panel.ZIndex="1" />
                        <Rectangle MinWidth="55" Height="30" StrokeThickness="1" Fill="Gainsboro" HorizontalAlignment="Left" Margin="30,30,0,0" />
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Declared Label in XAML:

<Label Background="Transparent" Width="200" Height="90" Content="Test your label" />

Output

enter image description here

Naturally, you will need to change the Template to fit your needs.

Note about several ways:

  • For a more sophisticated method and more advanced ways you can use your own Decorator, where with the help of DrawingContext you will draw your object. Example - How can I draw a border with squared corners in wpf?

  • Using the properties of the Geometry, where a Path, setting the desired shape of the figure. Example - WPF freeform border control.

like image 132
Anatoliy Nikolaev Avatar answered Aug 16 '26 20:08

Anatoliy Nikolaev