none
UserControl Dependency Binding to inner TextBox Text Property RRS feed

  • Question

  • Hi,

    I have been struggling to get this to work and after a days effort I turn to the forum guru(s) for help!

    I have a UserControl that contains a TextBox, I want to Bind a setting in the settings file to the Text property such that the Binding is TwoWay as hence the control persists its last value through instances.

    I had this working when hard coding the binding in the UserControl by merging the settings into the resources and accessing it as follows:

    Text="{Binding Source={StaticResource settings}, Path=Default.FilterText, Mode=TwoWay}"

    However I reuse the UserControl (shock) and I now need to bind to a different source for each use of control. I Created a dependency property called Filter and bound to that but it doesn't work as I expected it to.

    public static readonly DependencyProperty FilterProperty = 
                DependencyProperty.Register("Filter"
                                            typeof(String), 
                                            typeof(ParameterValueControl), 
                                            new UIPropertyMetadata(""
                                                new PropertyChangedCallback(OnFilterChanged), 
                                                new CoerceValueCallback(OnCoerceFilter)) 
                                            ); 


    Can some kind person show me how this SHOULD be done / where and how I can map the Filter to the TextBox.Text property such that they bind both ways.

    Many thanks.
    Oli

    Monday, September 15, 2008 3:05 PM

Answers

  • Thanks for the suggestions. I think I was over killing it with Callbacks on changes, coerce etc.. when it works with just this code.

    Control XAML:

    <UserControl> 
    ... 
    <TextBox x:Uid="PART_SearchField" x:Name="PART_SearchField" 
                         Grid.Row="0" Grid.Column="1" Width="300" VerticalAlignment="Center" Margin="10" 
                         Text="{Binding ElementName=UserControl_1 Path=Filter, Mode=TwoWay}}"/> 
    ... 
    </UserControl> 

    Control Code behind:
     
    public string Filter 
            { 
                get { return (string)GetValue(FilterProperty); } 
                set { SetValue(FilterProperty, value); } 
            } 
     
            // Using a DependencyProperty as the backing store for Filter.  This enables animation, styling, binding, etc... 
            public static readonly DependencyProperty FilterProperty = 
                DependencyProperty.Register("Filter"typeof(string), typeof(ParameterValueControl), new UIPropertyMetadata("")); 
     


    Window XAML:

    <c:MyControl DataContext="{StaticResource SecureParametersObj}"  
                 Filter="{Binding Source= StaticResource settings}, Path=Default.FilterCritical, Mode=TwoWay}" /> 


     
     
    • Marked as answer by Oli82 Tuesday, September 16, 2008 10:26 AM
    Tuesday, September 16, 2008 10:26 AM

All replies

  • You should just need to use a TemplateBinding. Like so:

    <TextBox ... Text="{TemplateBinding Filter}" ... /> 

    HTH,
    Drew
    • Proposed as answer by Jim Zhou - MSFT Wednesday, September 17, 2008 2:15 AM
    Monday, September 15, 2008 5:32 PM
    Moderator
  • If I suppose that the dependency property Filter is defined in the UserControl, you can try this :

    <TextBlock Text="{Binding Filter, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" /> 

    A simplest solution is to set the DataContext of the UserControl like this :

    public UserControl1()  
    {  
        InitializeComponent();  
     
        this.DataContext = this;  
     } 

    And then user a simplest DataBind expression :
    <TextBlock Text="{Binding Filter, Mode=TwoWay}" />   

    Vko
    Monday, September 15, 2008 8:02 PM
  • Thanks for the suggestions. I think I was over killing it with Callbacks on changes, coerce etc.. when it works with just this code.

    Control XAML:

    <UserControl> 
    ... 
    <TextBox x:Uid="PART_SearchField" x:Name="PART_SearchField" 
                         Grid.Row="0" Grid.Column="1" Width="300" VerticalAlignment="Center" Margin="10" 
                         Text="{Binding ElementName=UserControl_1 Path=Filter, Mode=TwoWay}}"/> 
    ... 
    </UserControl> 

    Control Code behind:
     
    public string Filter 
            { 
                get { return (string)GetValue(FilterProperty); } 
                set { SetValue(FilterProperty, value); } 
            } 
     
            // Using a DependencyProperty as the backing store for Filter.  This enables animation, styling, binding, etc... 
            public static readonly DependencyProperty FilterProperty = 
                DependencyProperty.Register("Filter"typeof(string), typeof(ParameterValueControl), new UIPropertyMetadata("")); 
     


    Window XAML:

    <c:MyControl DataContext="{StaticResource SecureParametersObj}"  
                 Filter="{Binding Source= StaticResource settings}, Path=Default.FilterCritical, Mode=TwoWay}" /> 


     
     
    • Marked as answer by Oli82 Tuesday, September 16, 2008 10:26 AM
    Tuesday, September 16, 2008 10:26 AM