views:

22

answers:

2

I have used DependencyProperty to pass value from textbox at silverlight page to external silverlight user control located in same page and that works fine.

But the problem is I could not bind the Textbox in page in reverse way. i.e. when i need to have changes in parent page when its usercontrol's textbox text is changed.

The XAML:

<TextBox x:Name="TextDataCollectionAccounts" Width="200" Height="25" VerticalAlignment="Top"></TextBox>                    
<Local:CalControl   x:Name="RCal" DateRange="{Binding ElementName=TextDataCollectionAccounts, Path=Text, Mode=TwoWay}" Visibility="Collapsed"/>

How to bind "TextDataCollectionAccounts"(in silverlight page) with Textbox of "Local:CalControl"?

A: 

Basically never try to bind to the inner workings of a user control. Treat it like it was a third-party control and expose properties and events.

  • Expose the Text property of the TextBox as a Text dependency property on your usercontrol.
  • Catch the change event of the TextBox and raise a property change event for the new Text dependency property.
  • Then you can simply bind to your new Text property on your user control.

If you need a specific example, just ask, but it sounds like you are almost there and know your DPs.

Enough already
Thanks for your answer. Yes it would be nice to have a example. My idea is to get text of "Local:CalControl"(say textChildControl.text) in parent page TextDataCollectionAccounts.text. I tried the change text event handling of my user control's text property but I might did wrong somewhere its still don't work as i expected.
webKite
@webKite: This sort of stuff is easy to get wrong. Only takes 1 break in the chain. Can you provide source? It is then easy to give actual example. If it is confidential you can always contact me through my company website.
Enough already
I have posted the code that i write for trying these things to work. Unfortunately its not working till now.
webKite
Finally, I figured out my mistakes and I solved them now. Thanks for you Help.
webKite
A: 

I did tried it following way but still not able to make it work:

page.xaml:

<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
<TextBox x:Name="TextDataCollectionAccounts" Width="200" Height="25" VerticalAlignment="Top"></TextBox>
<ToggleButton x:Name="ButtonShowHideCalander" Content="Date" IsThreeState="False" Width="100" Click="ToggleButton_Click" Height="25" VerticalAlignment="Top"></ToggleButton>
<Local:MyUserControl x:Name="RCalanderData" DateRange="{Binding ElementName=TextDataCollectionAccounts, Path=Text, Mode=TwoWay}" Visibility="Collapsed"/>  

page.xaml.cs

private MyUserControl uc=new Rlight.Views.ExportImport.Controls.MyUserControl();
    public page()
    {
        InitializeComponent();            
        uc.OkButton.Click+=new RoutedEventHandler(Button_Click_control);
    }

    private void Button_Click_control(object sender, RoutedEventArgs e)
    {
        TextDataCollectionAccounts.Text = uc.DateDatas;
    }

MyUserControl.xaml

<TextBox x:Name="ResultSelectedDates" Grid.Row="1" Text="{Binding DateRange, Mode=TwoWay}" Width="100"/><Button x:Name="OkButton" Content="Ok" Height="40" Width="102"></Button>

MyUserControl.xaml.cs

public partial class MyUserControl : UserControl
{
    public static readonly DependencyProperty DateRangeProperty =
       DependencyProperty.Register("DateRange", typeof(String), typeof(MyUserControl), new PropertyMetadata(DataRangeChanged));

    public MyUserControl()
    {
        InitializeComponent();
        this.DataContext = new AccountingViewModel();
    }

    public string DateRange
    {
        get
        {
            return (string)GetValue(DateRangeProperty);
        }
        set
        {
            SetValue(DateRangeProperty, value);
        }
    }
    private static void DataRangeChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        MyUserControl item = (source as MyUserControl);
        if (item != null) item.ResultSelectedDates.Text = e.NewValue.ToString();
    }
}
webKite