Reputation: 4393
I have made the following sample user control:
<UserControl x:Class="DLSAdministration.Controls.CombinedTextBoxControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="30"
d:DesignWidth="250"
mc:Ignorable="d">
<UserControl.Resources>
<SolidColorBrush x:Key="BGBrush" Color="#3A3A3A" />
<SolidColorBrush x:Key="BorderBrush" Color="#656565" />
</UserControl.Resources>
<Grid Width="{Binding Path=Width, ElementName=tb}" Height="{Binding Path=Height, ElementName=tb}">
<TextBox Name="tb"
Grid.Row="0"
Grid.Column="0"
Background="{StaticResource BGBrush}"
BorderBrush="{StaticResource BorderBrush}"
BorderThickness="2"
FontFamily="Calibri"
FontSize="16"
Foreground="Snow">
Content
</TextBox>
<TextBlock Margin="0,5,5,5"
HorizontalAlignment="Right"
FontStyle="Italic"
Foreground="Red"
Padding="2">
Label
</TextBlock>
</Grid>
With the code behind file:
public partial class CombinedTextBoxControl : UserControl
{
public CombinedTextBoxControl()
{
InitializeComponent();
}
// the exposed instance public properties.
public string TextboxText
{
get { return (string) GetValue(TextboxTextProperty); }
set { SetValue(TextboxTextProperty, value); }
}
public string TextBlockText
{
get { return (string)GetValue(TextBlockTextProperty); }
set { SetValue(TextBlockTextProperty, value); }
}
private static readonly DependencyProperty TextboxTextProperty =
DependencyProperty.Register
(
"TextboxText",
typeof (string),
typeof (CombinedTextBoxControl),
new FrameworkPropertyMetadata("TextboxText")
);
private static readonly DependencyProperty TextBlockTextProperty =
DependencyProperty.Register
(
"TextBlockText",
typeof(string),
typeof(CombinedTextBoxControl),
new FrameworkPropertyMetadata("TextBlockText ")
);
}
My UserControl is consumed like this:
<my:CombinedTextBoxControl x:Name="combinedTextBoxControl1"
Grid.Column="1"
Width="182"
Margin="77,76,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
TextBlockText="Name"
TextboxText="Ibrar Mumtaz" />
I have not even bound any data to this control and I can't even get it to display some sample data I provided via xaml? Can you spot the mistake? I have looked around on SO and noticed the mistakes of others but I am not making any obvious mistakes though so I am completely stumped on this one .... : S
Upvotes: 0
Views: 1556
Reputation: 11051
How do you expect it to work? You have a dependency property in your CombinedTextBoxControl but you don't use it anywhere. You want to assign this property to your child controls inside your control template, this is a way to do it:
Give your usercontrol a name
<UserControl x:Class="DLSAdministration.Controls.CombinedTextBoxControl"
x:Name="myControl"
and add a binding to your textbox and textblock like
Text="{Binding ElementName=myControl, TextBlockText}"
Just another note: your layout is wrong. You have a grid with only one cell, and you place a textblock and a textbox in it, one is placed on the other. Try adding 2 ColumnDefinitions one for the label, one for the textbox and set the proper attached property on both controls (Grid.Column="0" on the textblock, Grid.Column="1" on the textbox).
Upvotes: 1