views:

104

answers:

2

Hi I have my custom control which looks like that

<UserControl BorderBrush="#A9C2DE" HorizontalAlignment="Left" x:Class="Block"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="86" Width="151"  ToolTip="{DynamicResource BasicTooltip}">
<UserControl.Resources>
    <ResourceDictionary Source="TextBoxStyles.xaml"/>
</UserControl.Resources>
<DockPanel LastChildFill="True" Style="{StaticResource PanelStyle}">
    <Label DockPanel.Dock="Top" Content="{Binding Path=_Code}" HorizontalAlignment="Stretch"  Name="label1" Height="25" VerticalAlignment="Top" Style="{StaticResource LabelStyle}" ></Label>
    <TextBox  Name="txtBox"  Style="{StaticResource DefaultStyle}" >
        <TextBox.Text>
            <Binding Path="_Name">

            </Binding>
        </TextBox.Text>
    </TextBox>

</DockPanel>

So as You can see this control consist of a DockPanel where I placed label and textbox. In code I added some event to an operation on label and textbox mentioned above. This control has a basic shape of rectangle. However today I found out that it would be better for this control to have shape of rhombus or sth more sophisticated then casual rectangle. Is it possible to give my control different shape, keep all functionality (all events I wrote in code file) and keep the content (textbox and label) intanct ?

I gave a try to this code

 <Style TargetType="{x:Type UserControl}" x:Key="BlockStyle" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>

                    <Ellipse  
                            x:Name="Border"  
                            Stroke="#FF393785"  
                            StrokeThickness="2" 
                            Fill="Transparent"
                            >

                    </Ellipse>
            </ControlTemplate>
            </Setter.Value>

        </Setter>
</Style>

However when I use this style in my control, all elements (textbox and label etc) are coverd by this style.

A: 

Use Border insted and add what you want in template (TextBlock etc.)

<ControlTemplate TargetType="UserControl">                           
    <Border x:Name="border" BorderThickness="2" CornerRadius="15" BorderBrush="#FF211c19" RenderTransformOrigin="0.5,0.5">
             <!--I use binding to show content of control as a text in TextBlock-->
        <TextBlock TextWrapping="Wrap" Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,5"/>
    </Border>
</ControlTemplate>
lukas
It seems to me that it's not working. If I transfer my textboxes etc to this code I get errors in c# code(compiler do not see textboxes etc which I refer to in my code). I would like to have an resourcedictionary(actually put code above into resource dictionary) where I would keep styles (which wouuld be responsible for shape of control). Content of control never changes , I need only the control border shape to change into rhombus.
elMariachi
A: 

This is actually simpler then you think, no control template required:

  1. Set the user control's Background property to {x:Null}, this make the background "transparent" to the mouse (the mouse events will be handled by whatever is below the user control).

  2. Create the element that defined the control's shape, give it a non-null background (transparent is fine).

  3. if you can put the control content inside the element (for example if it's a Border) do it, otherwise put both the shape and your content in a single cell Grid and use Margin to move the content into the shape.

So, your user control as an ellipse becomes:

<UserControl HorizontalAlignment="Left" x:Class="Block"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="86" Width="151"  ToolTip="{DynamicResource BasicTooltip}"
    Background="{x:Null}">                          <-- set background to null
    <UserControl.Resources>
        <ResourceDictionary Source="TextBoxStyles.xaml"/>
    </UserControl.Resources>
    <Grid>                                          <-- the container grid
        <Ellipse                                    <-- the control shape
            x:Name="Border"  
            Stroke="#FF393785"  
            StrokeThickness="2" 
            Fill="Transparent"/>                    <-- with a non-null background

        <DockPanel                                  <-- actual content
            LastChildFill="True" 
            Style="{StaticResource PanelStyle}" 
            Margin="10 18 10 23">                   <-- pushed inside the ellipse

            ...

        </DockPanel>
    </Grid>
</UserControl>
Nir
It looks great but I tried to make a rhombus with Polygon class, but it doesn't streach to the grid container(just like elipse does). What can I do then ?
elMariachi