tags:

views:

140

answers:

1

Hello

I am trying to convert this xaml textbox with validation into C# so that it can be dynamically created and populated from code. I am getting stuck creating the validation bindings. Can anyone provide any hints?

<TextBox Height="20" Width="200" >
      <Binding RelativeSource="{x:Static RelativeSource.Self}" Path="Text" >
           <Binding.ValidationRules>
                <runtime:StandardTextBoxValidationRule/>
           </Binding.ValidationRules>
       </Binding>
</TextBox>
+1  A: 

You can do it like so:

TextBox textBox = // Get or create the text box

var binding = new Binding();
binding.Source = RelativeSource.Self;
binding.Path = new PropertyPath("Text");
binding.ValidationRules.Add(new StandardTextBoxValidationRule());
textBox.SetBinding(TextBox.TextProperty, binding);
Reed Copsey
I had to add the following to get it workingbinding.Path = new PropertyPath("Text");Thanks again.
KithKann
Glad it worked. I added that to my answer, as well, in case other poeple find this.
Reed Copsey
You can also use `new Binding("Text")`
Pavel Minaev