【问题标题】:Setting binding on usercontrol to mainwindow's ViewModel在用户控件上设置绑定到主窗口的 ViewModel
【发布时间】:2015-06-17 13:35:23
【问题描述】:

我有以下用户控制

<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
        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 数据绑定到此属性。

谢谢。

【问题讨论】:

    标签: .net wpf xaml mvvm data-binding


    【解决方案1】:

    在您的 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}}}" />
    

    在您看来,只需将此新的 Text 属性绑定到您现有的 SelectedRefDesFilter 属性即可。

    <controls:FilterTraceDataControl Grid.Row="1" Visibility="{Binding FilterBarVisible ,Converter={StaticResource BooleanToVisibilityConverter}, Mode=TwoWay}"
                                     Text="{Binding SelectedRefDesFilter, Mode=TwoWay}" />
    

    【讨论】:

    • 非常感谢。
    【解决方案2】:
    "{Binding DataContext.SelectedRefDesFilter, 
                                RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
    

    这应该可以,但我不知道您的 UserControl 中的属性将绑定到 Window 中现有的属性。绑定语法应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多