1

I have following user control

<UserControl x:Class="Station.Controls.FilterTraceDataControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d"> <TextBox x:Name="PartNumbTextBox" Width="120" VerticalAlignment="Bottom" HorizontalAlignment="Left" Margin="5,0,0,0" Height="25"/> </UserControl> 

I use it with my main window:

<Window xmlns:controls="clr-namespace:Station.Controls" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="Station.MainWindow" Title="{Binding ApplicationTitle}" MinHeight="550" MinWidth="850" Height="650" Width="900" DataContext="{Binding Main, Source={StaticResource Locator}}"> <controls:FilterTraceDataControl Grid.Row="1" Visibility="{Binding FilterBarVisible ,Converter={StaticResource BooleanToVisibilityConverter}, Mode=TwoWay}"/> </Window> 

Window's Mainview has following property:

public string SelectedRefDesFilter { get { return _selectedRefDesFilter; } set { _selectedRefDesFilter = value; RaisePropertyChanged("SelectedRefDesFilter"); } } 

How I can databind "PartNumbTextBox" from UserControl to this Property.

Thanks.

2 Answers 2

1

On your UserControl's code-behind (FilterTraceDataControl.xaml.cs), add a DependencyProperty, like this:

public string Text { get { return (string)this.GetValue(TextProperty); } set { this.SetValue(TextProperty, value); } } public static readonly DependencyProperty TextProperty = DependencyProperty.Register( "Text", typeof(string), typeof(FilterTraceDataControl),new PropertyMetadata(null)); 

Then bind your UserControl's TextBox to it by RelativeSource or ElementName:

<TextBox x:Name="PartNumbTextBox" Width="120" VerticalAlignment="Bottom" HorizontalAlignment="Left" Margin="5,0,0,0" Height="25" Text="{Binding Text, RelativeSource={RelativeSource AncestorType={x:Type controls:FilterTraceDataControl}}}" /> 

And in your view, just bind this new Text property to your existing SelectedRefDesFilter property.

<controls:FilterTraceDataControl Grid.Row="1" Visibility="{Binding FilterBarVisible ,Converter={StaticResource BooleanToVisibilityConverter}, Mode=TwoWay}" Text="{Binding SelectedRefDesFilter, Mode=TwoWay}" /> 
Sign up to request clarification or add additional context in comments.

Comments

0
"{Binding DataContext.SelectedRefDesFilter, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" 

This should work, but i don't know the property in your UserControl which will be bounded to the one existing in the Window. The binding syntax should work.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.