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.
string bnd = "Model.+ lok.Name +.Value";This is incorrect syntax for binding. What are you actually trying to bind to? Your question is unclear.