2

I Have the following XAML in UserControl:

<StackPanel Orientation="Horizontal" Name="spContainer">
    <TextBlock x:Name="tbLabelBefore" MinWidth="50" Text="{Binding LabelBefore}"></TextBlock>
    <TextBox Name="txtKey" MinWidth="120"></TextBox>
    <TextBlock Name="tbValue" MinWidth="50"></TextBlock>
</StackPanel>

Next I want to set binding dynamically to Text property on the TextBox-txtKey from proxy class.

I Do the following:

MDLookup lok = SelectedObject as MDLookup;

string bnd = "Model."+ lok.Name +".Value";            
Binding binding = new Binding(bnd);
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
//binding.ValidatesOnDataErrors = true;
//binding.NotifyOnValidationError = true;
binding.Mode = BindingMode.TwoWay;
lok.TxtKey.SetBinding(TextBox.TextProperty, binding);

Here lok is instance of my user control. And TxtKey is Property in my UserControl of type TextBox that returns the txtKey element:

[XmlIgnore]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public TextBox TxtKey
{
    get { return this.txtKey; }
    set { this.txtKey = value; }
}

If I put:

lok.TxtKey.Text = "Some Text"

This works. Also this code for setting binding works in my constructor of my user control. But here doesn't. Any idea why?

Additional: I have ovverided ShouldSerializeContent

public override bool ShouldSerializeContent()
{
    return false;
}

The point is I serialize xaml from multiple controls in databasebase and afterwards dynamically load and set DataContext.

2
  • string bnd = "Model.+ lok.Name +.Value"; This is incorrect syntax for binding. What are you actually trying to bind to? Your question is unclear. Commented Jul 8, 2015 at 7:29
  • that is binding path, it can be any string. That is not the problem. And it is correct syntax I just forgot the quotes: string bnd = "Model."+ lok.Name +".Value"; My DataContext has dynamic property Model and in it's collection I have model named dynamically lok.Name and property Value, so the path is correct. Commented Jul 8, 2015 at 7:38

1 Answer 1

1

If TxtKey is the name of a TextBox inside your UserControl, then you are having problems because you cannot access elements that are inside controls from other controls. Although you can access named controls from inside the UserControl that they were defined in, this does not mean that they are publicly available from outside the control.

If TxtKey is a DependencyProperty of type UserControl, then you will find that the UserControl class does not have a Text property in it and so you will still not be able to bind to it. I cannot answer further without you providing further information. In these cases listed above, you should have received some sort of compilation or binding error... check your Output window in Visual Studio for errors.


UPDATE >>>

In order to achieve what you want, you need to define a DependencyProperty in your UserControl. I won't show you how to do that here as there are millions of online examples... let's say you name it Text. Change the XAML in your UserControl to this:

<TextBox Name="txtKey" MinWidth="120" Text="{Binding Text, RelativeSource={RelativeSource 
    AncestorType={x:Type YourPrefix:YourUserControl}}}" />

Then you will have a publicly available property to data bind to or set externally that will update the TextBox.Text value:

<YourPrefix:YourUserControl Text="{Binding DataBoundTextPropertyOutsideControl}" />

Or:

<YourPrefix:YourUserControl Text="Plain text string" />

Or in code:

YourUserControl yourUserControl = new YourUserControl();
yourUserControl.Text = "Plain text string";

Or:

...
yourUserControl.SetBinding(YourUserControl.TextProperty, binding);
Sign up to request clarification or add additional context in comments.

1 Comment

I Updated the content of the question. The problem is that I can access lok.TxtKey.Text, but I cannot set binding to it(from outside the control itself).

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.