Thursday, December 20, 2012

Silverlight - Strategy passing Data to a User Control

Silverlight - Strategy passing Data to a User Control

Silverlight5

From a button click on Tab1Data I want to pass a parameter to Tab2_1Data

Am currently doing it with a global public property(!) on the MainPage which each UserControl can reference using this helper

Is there a better way using a non MVVM approach to pass parameters?

 <controls:TabControl Name="TabOverallMain">                         <!-- Tab 1 -->                         <controls:TabItem Header="Home Screen" IsSelected="True">                             <UserControls:Tab1Data />                         </controls:TabItem>                          <!-- Tab 2 -->                         <controls:TabItem Header="Admin">                             <Grid>                                 <controls:TabControl>                                     <!-- Tab 2_1 -->                                     <controls:TabItem Header="Users">                                         <UserControls:Tab2_1Data />                                     </controls:TabItem> 

Answers & Comments...

Answer: 1

Since you dont want to use MVVM, try use data binding.. You will need to expose the parameter as a dependency propety int the first control

Here's some pseudo code. Create a dependency property on Tab1Data called MyParam

    public static readonly DependencyProperty MyParamProperty = DependencyProperty.Register("MyParam",                 int, typeof(Tab2_1Data), null);  public int MyParam {     get { return (int)GetValue(MyParamProperty); }     set     {         SetValue(MyParamProperty, value);     } }  <controls:TabItem Header="Home Screen" IsSelected="True">     <UserControls:Tab1Data x:Name="OneCtrl"  /> </controls:TabItem> 

Then create a dependency property (similar to above) on the second control called something like ParamFromControlOne... then bind them

<UserControls:Tab2_1Data ParamFromControlOne="{Binding MyParam, ElementName=OneCtrl}" /> 

That's not tested and thrown together using Notepad, but that is the path I would go down

by : tsiornhttp://stackoverflow.com/users/223155




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog