将用户控件绑定到主窗口的 ViewModel
Setting binding on usercontrol to mainwindow's ViewModel
我有以下用户控制
<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>
我将它与我的主程序一起使用 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 的 Mainview 有以下 属性:
public string SelectedRefDesFilter
{
get { return _selectedRefDesFilter; }
set
{
_selectedRefDesFilter = value;
RaisePropertyChanged("SelectedRefDesFilter");
}
}
如何将 "PartNumbTextBox" 从 UserControl 数据绑定到此 属性。
谢谢。
"{Binding DataContext.SelectedRefDesFilter,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
这应该可行,但我不知道您的 UserControl 中的 属性 将绑定到 Window 中存在的那个。绑定语法应该有效。
在您的 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));
然后通过 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}}}" />
并且在您看来,只需将此新文本 属性 绑定到您现有的 SelectedRefDesFilter 属性。
<controls:FilterTraceDataControl Grid.Row="1" Visibility="{Binding FilterBarVisible ,Converter={StaticResource BooleanToVisibilityConverter}, Mode=TwoWay}"
Text="{Binding SelectedRefDesFilter, Mode=TwoWay}" />
我有以下用户控制
<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>
我将它与我的主程序一起使用 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 的 Mainview 有以下 属性:
public string SelectedRefDesFilter
{
get { return _selectedRefDesFilter; }
set
{
_selectedRefDesFilter = value;
RaisePropertyChanged("SelectedRefDesFilter");
}
}
如何将 "PartNumbTextBox" 从 UserControl 数据绑定到此 属性。
谢谢。
"{Binding DataContext.SelectedRefDesFilter,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
这应该可行,但我不知道您的 UserControl 中的 属性 将绑定到 Window 中存在的那个。绑定语法应该有效。
在您的 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));
然后通过 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}}}" />
并且在您看来,只需将此新文本 属性 绑定到您现有的 SelectedRefDesFilter 属性。
<controls:FilterTraceDataControl Grid.Row="1" Visibility="{Binding FilterBarVisible ,Converter={StaticResource BooleanToVisibilityConverter}, Mode=TwoWay}"
Text="{Binding SelectedRefDesFilter, Mode=TwoWay}" />