Detallando @Steven Su respuesta, en su UserControl defina DependencyProperty
. La definición de un DependencyProperty
permite cambiar las notificaciones para que activen las actualizaciones de sus controles.
En el código subyacente de su UserControl puede agregar la propiedad de dependencia.
public partial class MyUserControl : UserControl
{
public string TextBlockText
{
get { return (string)GetValue(TextBlockTextProperty); }
set { SetValue(TextBlockTextProperty, value); }
}
public static readonly DependencyProperty TextBlockTextProperty =
DependencyProperty.Register("TextBlockText", typeof(string), typeof(MyUserControl), new UIPropertyMetadata(""));
public MyUserControl()
{
InitializeComponent();
DataContext = this;
}
}
Esto expone a un público DependencyProperty
que puede unirse a en XAML de su control de usuario.
<UserControl>
<TextBlock Text="{Binding Path=TextBlockText}" />
</UserControl>
Ahora necesita una forma de configurar esa propiedad desde su control de ventana. Voy a detalle tres maneras en que usted puede hacer esto:
1.) Ya que la propiedad TextBlockText
se expone en el control de usuario que puede establecer directamente en el XAML como:
<Window x:Class="WpfApplication2.MainWindow"
xmlns:local="clr-namespace:WpfApplication2">
<local:MyUserControl TextBlockText="Text that you want to set.">
</local:MyUserControl>
</Window>
2.) Si damos el control de usuario un nombre podemos cambiar la propiedad dentro de la ventana de código subyacente:
<Window x:Class="WpfApplication2.MainWindow"
xmlns:local="clr-namespace:WpfApplication2">
<local:MyUserControl Name="CoolUserControl">
</local:MyUserControl>
</Window>
-
CoolUserControl.TextBlockText = "Text that you want to set.";
3.) O, por último, puede crear otro DependencyProperty
dentro del código subyacente de Window
y vincularlo a la propiedad de dependencia UserControl
. De esta forma, cada vez que actualice la propiedad, el valor dentro de su código Window
la propiedad de dependencia UserControl
también cambiará. Esta es la opción preferible como @Steven que dijiste antes, ya que tu código detrás no necesita saber sobre ningún control.
public partial class MainWindow : Window
{
public string UserControlText
{
get { return (string)GetValue(UserControlTextProperty); }
set { SetValue(UserControlTextProperty, value); }
}
public static readonly DependencyProperty UserControlTextProperty =
DependencyProperty.Register("UserControlText", typeof(string), typeof(MainWindow), new UIPropertyMetadata(""));
public MainWindow()
{
InitializeComponent();
DataContext = this;
UserControlText = "Text that you want to set.";
}
}
Y para unirse a nuestro nuevo DependencyProperty
en el XAML Ventana:
<Window x:Class="WpfApplication2.MainWindow"
xmlns:local="clr-namespace:WpfApplication2">
<local:MyUserControl TextBlockText="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}, Path=UserControlText}"></local:MyUserControl>
</Window>
Espero que esto ayude!
¿Puedes dar un pequeño ejemplo de cómo hacerlo? –
Lea esto: http: //www.codeproject.com/Articles/224230/Exploring-the-use-of-Dependency-Properties-in-User –
Eche un vistazo a este enlace de datos [ejemplo] (http://code.msdn.microsoft.com/windowsapps/Data-Binding- 7b1d67b5) –