Silverlight not initializing controls
I'm trying to add some values in several controls in my silverlight page (textboxes, datepickers...) but it seems like some of them are never initialize (until user sets any value manually).
I've tried it by been suscribed to the "Loaded" event in the page's code behind but it doesn't work... (I thought it was because of the order it was being initialized).
I've read that this is one of silverlight limitations but maybe there is any workaround...
Any idea?? Thanks in advance.
Some code to illustrate it, first the xaml controls:
<sdk:Label Target="{Binding ElementName=txtNumeroColegiado}" Content="Nº Colegiado" HorizontalAlignment="Right" Margin="5,0" Name="label2" VerticalAlignment="Center" />
<TextBox Margin="5,0" Name="txtNumeroColegiado" TabIndex="27" Text="{Binding Medico.colegiado, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True, ValidatesOnDataErrors=True}" VerticalAlignment="Center" Grid.Column="1" />
<sdk:Label Target="{Binding ElementName=txtNombreComercial}" Content="Nombre Comercial" HorizontalAlignment="Left" Margin="5,0" Name="label4" VerticalAlignment="Center" Grid.Column="2" />
<TextBox Margin="5,0" Name="txtNombreComercial" TabIndex="27" Text="{Binding Medico.nombrecomercial, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True, ValidatesOnDataErrors=True}" VerticalAlignment="Center" Grid.Column="3" />
And this is my conde behind:
public DatosMedico()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(DatosMedico_Loaded);
}
void DatosMedico_Loaded(object sender, RoutedEventArgs e)
{
this.txtNombreComercial.GetBindingExpression(TextBox.TextProperty).UpdateSource();
this.txtNumeroColegiado.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
When it tries to run "updateSource", these textboxes are null (but they are working perfectly in the page...). Apparently they are initialized when user inputs some text...
At guess, the DataContext
is either not being set or not set to the object you are expecting it to be set to. Check the output window in VS when debugging, do you see any complaints about binding?
它通过删除一个内部有很多控件的“ContentPresenter”来解决。
链接地址: http://www.djcxy.com/p/22792.html上一篇: Silverlight + MVVM +绑定=内存泄漏?
下一篇: Silverlight没有初始化控件