Reputation: 143
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.
Upvotes: 2
Views: 3265
Reputation: 69959
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);
Upvotes: 1