在用户控件上设置绑定到主窗口的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
        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数据绑定到此属性。

谢谢。

阿尔穆洛

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从 WPF 中的主窗口引发用户控件上的事件

来自分类Dev

我可以通过绑定到wpf父控件上的属性的依赖项属性在xaml中设置用户控件datacontext吗?

来自分类Dev

如何在用户控件和父窗口之间绑定WPF命令

来自分类Dev

在用户控件中定义命令绑定

来自分类Dev

单击主窗体上的按钮时,在用户控件中隐藏面板

来自分类Dev

在用户控件内设置Treeview的ItemTemplateSelector

来自分类Dev

如何在用户控件上公开组合框数据绑定

来自分类Dev

在用户控件上更改光标

来自分类Dev

在用户控件上动态选择矩形

来自分类Dev

在用户控件上更改光标

来自分类Dev

在用户窗口屏幕末尾设置Div

来自分类Dev

从 ViewModel 到 Custom-Control 的多阶段绑定到 ControlTemplate 上的控件不起作用

来自分类Dev

如何将用户控件的属性绑定到MainViewModel并将其数据上下文绑定到其自己的ViewModel?

来自分类Dev

将ItemsControl中的viewmodel绑定到自定义用户控件

来自分类Dev

WPF数据将ViewModel属性绑定到用户控件内部的ListBox中,背后的代码

来自分类Dev

ViewModel 和 View 未绑定到列表框中的用户控件

来自分类Dev

将窗口图标绑定到图像控件

来自分类Dev

将弹出窗口绑定到控件

来自分类Dev

窗口ResizeMode绑定到设置

来自分类Dev

在用户控件中绑定ObservableCollection依赖项属性

来自分类Dev

将ViewModel绑定到多个窗口

来自分类Dev

WPF从UserControl视图模型绑定主窗口控件

来自分类Dev

将属性绑定到用户控件

来自分类Dev

将用户控件属性绑定到ItemsControl

来自分类Dev

从子用户控件绑定到ElementName

来自分类Dev

数据绑定到定期用户控件

来自分类Dev

将用户控件绑定到itemcontrol中

来自分类Dev

从ResourceDictionary到用户控件的绑定样式

来自分类Dev

在按钮工具提示上设置绑定,将用户控件的声明实例作为源

Related 相关文章

  1. 1

    从 WPF 中的主窗口引发用户控件上的事件

  2. 2

    我可以通过绑定到wpf父控件上的属性的依赖项属性在xaml中设置用户控件datacontext吗?

  3. 3

    如何在用户控件和父窗口之间绑定WPF命令

  4. 4

    在用户控件中定义命令绑定

  5. 5

    单击主窗体上的按钮时,在用户控件中隐藏面板

  6. 6

    在用户控件内设置Treeview的ItemTemplateSelector

  7. 7

    如何在用户控件上公开组合框数据绑定

  8. 8

    在用户控件上更改光标

  9. 9

    在用户控件上动态选择矩形

  10. 10

    在用户控件上更改光标

  11. 11

    在用户窗口屏幕末尾设置Div

  12. 12

    从 ViewModel 到 Custom-Control 的多阶段绑定到 ControlTemplate 上的控件不起作用

  13. 13

    如何将用户控件的属性绑定到MainViewModel并将其数据上下文绑定到其自己的ViewModel?

  14. 14

    将ItemsControl中的viewmodel绑定到自定义用户控件

  15. 15

    WPF数据将ViewModel属性绑定到用户控件内部的ListBox中,背后的代码

  16. 16

    ViewModel 和 View 未绑定到列表框中的用户控件

  17. 17

    将窗口图标绑定到图像控件

  18. 18

    将弹出窗口绑定到控件

  19. 19

    窗口ResizeMode绑定到设置

  20. 20

    在用户控件中绑定ObservableCollection依赖项属性

  21. 21

    将ViewModel绑定到多个窗口

  22. 22

    WPF从UserControl视图模型绑定主窗口控件

  23. 23

    将属性绑定到用户控件

  24. 24

    将用户控件属性绑定到ItemsControl

  25. 25

    从子用户控件绑定到ElementName

  26. 26

    数据绑定到定期用户控件

  27. 27

    将用户控件绑定到itemcontrol中

  28. 28

    从ResourceDictionary到用户控件的绑定样式

  29. 29

    在按钮工具提示上设置绑定,将用户控件的声明实例作为源

热门标签

归档