Accessing an object of a tabitem in Silverlight
I have a Silverlight Usercontrol where I have a tabcontrol which uses a couple of tabitems. Now each tabitem is another Usercontrol. I was wondering if there is a way to access an object of one of these usercontrol tabitems.
For example if I have a xaml in my main usercontrol:
<controls:TabControl x:Name="TabControl" Grid.Row="1" Grid.Column="1" Foreground="#234BC3">
<controls:TabItem Header="Prestaties" x:Name="TabPres" Visibility="Collapsed">
<nsl:PrestatiesUC></nsl:PrestatiesUC>
</controls:TabItem>
<controls:TabItem Header="Protocollen" x:Name="TabProt" Visibility="Collapsed">
<nsl:ProtocollenUC></nsl:ProtocollenUC>
</controls:TabItem>
<controls:TabItem Header="Adt" x:Name="TabAdt" Visibility="Collapsed">
<nsl:AdtUC></nsl:AdtUC>
</controls:TabItem>
</controls:TabControl>
And in my PrestatiesUC usercontrol i have an object:
<Button x:Name="btnReSend" Content="Resend" Width="75" Height="25" Margin="10" Click="resend_Button"/>
How would I go about disabling it's visibility on startup dynamicly in the main usercontrol code?
I tried things like:
Prest开发者_Python百科atiesBAMUC tmp = new PrestatiesBAMUC();
tmp.btnReSend.Visibility = Visibility.Collapsed;
But this didn't work.
Any ideas?
In the main user control Loaded event this should work:-
((PrestatiesUC)TabPres.Content).btnReSend.Visibility = Visibility.Collapsed;
However it smells bad. Its generally not a good idea to have something like the main page have such intimate knowledge of how a UserControl is structured internally.
Would it not be better to have the "Prestaties" set the buttons visibility in is Loaded event?
If not the create some Interface that is implemented by PrestatiesUC (and likely the other Usercontrols involved). During Loaded in the main page simply enumerates over the set of tabs and calls a method on this interface passing in some state object. The Usercontrols then make choices about that status of the controls it contains.
If that seems over the top then at least add a property to the user control to hide the button itself:-
// In PrestiesUC
public bool ReSendVisible
{
get { return btnReSend.Visibility == Visibility.Visible; }
set { btnReSend.Visibility = value ? Visibility.Visible : Visibility.Collapsed; }
}
Now your code in the main user control would look like:-
((PrestatiesUC)TabPres.Content).ReSendVisible = False;
精彩评论