0

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;
}
4
  • Are you setting the DataContext? Commented Mar 3, 2014 at 14:43
  • Yes i did it in Design but here i added it in code now. Commented Mar 3, 2014 at 14:46
  • 1
    What exactly is your problem? Your code works fine for me. Do you perhaps need the binding to be updated on every key stroke in the CCs TextBox? Commented Mar 3, 2014 at 14:56
  • Yes when i type in the Textbox the Label should update directly Commented Mar 3, 2014 at 15:00

3 Answers 3

3

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}" />
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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); }
   }

2 Comments

default(string) is null. Hence your code does the same as that in the question.
Yes my mistake, I got fooled by an error while trying this code and recreated the dependency property with ReSharper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.