I need a custom control (CC) where the Text property of the textbox in the CC can be binded to a DependancyProperty on the CC.
I also tried to bind the textbox.text with templatebinding.
I tried almost everything i can think of, what to do? :
Custom Control in Generics:
<Style TargetType="{x:Type local:TextboxCC}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:TextboxCC}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<TextBox Text="{Binding NText, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:TextboxCC}}}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
CC.cs
public class TextboxCC : Control
{
public string NText
{
get { return (string)GetValue(NTextProperty); }
set { SetValue(NTextProperty, value); }
}
// Using a DependencyProperty as the backing store for NText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NTextProperty =
DependencyProperty.Register("NText", typeof(string), typeof(TextboxCC), new PropertyMetadata(null));
static TextboxCC()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TextboxCC), new FrameworkPropertyMetadata(typeof(TextboxCC)));
}
}
MainWindow:
The Label is just there to check if the value is changed in Mainwindow.Test
<local:TextboxCC NText="{Binding Test,Mode=TwoWay}" HorizontalAlignment="Left" Margin="115,99,0,0" VerticalAlignment="Top" Width="206"/>
<Label Content="{Binding Test,Mode=TwoWay}" HorizontalAlignment="Left" Margin="115,161,0,0" VerticalAlignment="Top" Width="206"/>
Mainwindow.cs
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string _test;
public string Test
{
get { return _test; }
set
{
_test = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Test"));
}
}
public MainWindow()
{
InitializeComponent();this.DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
}