[英]Setting binding on usercontrol to mainwindow's ViewModel
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: Window的Mainview具有以下属性:
public string SelectedRefDesFilter
{
get { return _selectedRefDesFilter; }
set
{
_selectedRefDesFilter = value;
RaisePropertyChanged("SelectedRefDesFilter");
}
}
How I can databind "PartNumbTextBox" from UserControl to this Property. 如何将“ PartNumbTextBox”从UserControl数据绑定到此属性。
Thanks. 谢谢。
On your UserControl's code-behind (FilterTraceDataControl.xaml.cs), add a DependencyProperty, like this: 在您的UserControl的代码隐藏(FilterTraceDataControl.xaml.cs)上,添加一个DependencyProperty,如下所示:
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: 然后通过RelativeSource或ElementName将UserControl的TextBox绑定到它:
<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. 在您看来,只需将此新的Text属性绑定到您现有的SelectedRefDesFilter属性即可。
<controls:FilterTraceDataControl Grid.Row="1" Visibility="{Binding FilterBarVisible ,Converter={StaticResource BooleanToVisibilityConverter}, Mode=TwoWay}"
Text="{Binding SelectedRefDesFilter, Mode=TwoWay}" />
"{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. 这应该工作,但我不知道您的UserControl中的属性将绑定到Window中现有的属性。 The binding syntax should work.
绑定语法应该起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.