Saneesh
Saneesh

Reputation: 33

Passing values to Control Template dependency property

How can I set the value of CornerRadious in Control template value taken from the custom control definition ... Please help XAML for User Control Is

<UserControl x:Class="Biz10.RoundedCornerTextBox"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:Biz10"
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
        <UserControl.Resources>
            <ControlTemplate x:Key="TextBoxBaseControlTemplate" TargetType="{x:Type TextBoxBase}">
                <Border Background="{TemplateBinding Background}" 
                    x:Name="Bd" BorderBrush="Gray"
                    BorderThickness="1" **CornerRadius="5"**>
                    <ScrollViewer x:Name="PART_ContentHost"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" TargetName="Bd"/>
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                    <Trigger Property="Width" Value="Auto">
                        <Setter Property="MinWidth" Value="100"/>
                    </Trigger>
                    <Trigger Property="Height" Value="Auto">
                        <Setter Property="MinHeight" Value="20"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </UserControl.Resources>
        <Grid>
            <TextBox Template="{StaticResource TextBoxBaseControlTemplate}" Height="25" Margin="5"></TextBox>
        </Grid>
    </UserControl>

C# Code file is

using System.Windows.Media.Imaging;
using System.Windows.Navigation;

using System.Windows.Shapes;

namespace Biz10
{
    /// <summary>
    /// Interaction logic for CornerTextBox.xaml
    /// </summary>
    public partial class RoundedCornerTextBox : UserControl
    {
        public static readonly DependencyProperty _cornrerRadious= DependencyProperty.Register("CornerRadious", typeof(int), typeof(RoundedCornerTextBox));
    public RoundedCornerTextBox()
    {
        InitializeComponent();
    }
    public int CornerRadious
    {
        get
        {
            return (int)GetValue(_cornrerRadious);
        }
        set
        {
            SetValue(_cornrerRadious, value);
        }
    }
}

}

I want to declare a custom control in Window by

<custome:RoundedCornerTextBox CornerRadious="7" Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="7" x:Name="txtAccountName" ></custome:RoundedCornerTextBox>

Is it Possible

Upvotes: 1

Views: 1391

Answers (2)

AnjumSKhan
AnjumSKhan

Reputation: 9827

You have to change the Template of your UserControl like below :

<UserControl.Template>
    <ControlTemplate TargetType="UserControl">
        <Border BorderThickness="3" 
                BorderBrush="#FFF0B0B0"
                CornerRadius="{Binding CornerRadius, RelativeSource={RelativeSource AncestorType=UserControl}}">
            <Grid>
                <TextBox Width="100"/>
            </Grid>
        </Border>
    </ControlTemplate>
</UserControl.Template>

See how the CornerRadius property is binded.

Upvotes: 1

atomaras
atomaras

Reputation: 2568

You don't need the enclosing UserControl. Just make a Custom Control (http://www.wpftutorial.net/howtocreateacustomcontrol.html) called RoundedCornerTextBox that

  1. extends TextBox
  2. changes its template to what you need
  3. exposes a new CornerRadius dependency property and finally
  4. uses TemplateBinding to connect this property to the Border inside your template.

Upvotes: 0

Related Questions