Question

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;
}
Was it helpful?

Solution

By default a binding on a TextBox's Text property is only updated (in source direction) when the control loses focus. If you want it to be updated each time the text changes, you can set the binding's UpdateSourceTrigger property to PropertyChanged:

<TextBox Text="{Binding NText,
    RelativeSource={RelativeSource FindAncestor, AncestorType=local:TextboxCC},
    UpdateSourceTrigger=PropertyChanged}" />

OTHER TIPS

You need to implement something like this:

void ExtendedCheckBox_Checked(object sender, RoutedEventArgs e)
    {
        if (!chk_IsChanging)
            this.IsCheckedReal = "X";                      

    }

    public string IsCheckedReal
    {
        get { return (string)GetValue(IsCheckedRealProperty); }
        set
        {
            SetValue(IsCheckedRealProperty, value);
        }
    }

    public static readonly DependencyProperty IsCheckedRealProperty =
        DependencyProperty.Register("IsCheckedReal", typeof(string), typeof(ExtendedCheckBox), new PropertyMetadata(IsCheckedRealChanged));

    private static void IsCheckedRealChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {

        if (e.NewValue.Equals("X") || e.NewValue.Equals("Y"))
        {
            ((ExtendedCheckBox)o).IsChecked = true;
        }
        else if (e.NewValue.Equals("") || e.NewValue.Equals("N"))
        {
            ((ExtendedCheckBox)o).IsChecked = false;
        }


    }

I want the behaviour that on checking of checkbox, I want a "X" mark in my model property.

So I have created a dependency property IsCheckedRealProperty that I will bind like this:

chk.SetBinding(ExtendedCheckBox.IsCheckedRealProperty, binding);

Hope this helps.

Change the implementation of your dependency property to this, and your code will work as it is. The textbox is updated on lost focus.

public static readonly DependencyProperty NTextProperty = DependencyProperty.Register(
       "NText", typeof (string), typeof (TextboxCC), new PropertyMetadata(default(string)));

   public string NText
   {
       get { return (string) GetValue(NTextProperty); }
       set { SetValue(NTextProperty, value); }
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top