Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从主窗口访问用户控件视图模型_C#_Wpf_Mvvm - Fatal编程技术网

C# 从主窗口访问用户控件视图模型

C# 从主窗口访问用户控件视图模型,c#,wpf,mvvm,C#,Wpf,Mvvm,我试图实现一组控件,这些控件将由应用程序中的各种仪器包装器/适配器使用。这些控件的目标是提供所有连接设备都会使用的功能,例如验证IP地址,而不是在添加新设备支持时让每个开发人员编写自己的。我们也在考虑将WPF和MVVM作为重新分解的一部分。我很难理解的问题是,来自主窗口的vm如何与控件的vm共享本例中验证的IP地址的数据。验证IP地址后,控制vm是否会将其写回模型,并且主窗体vm会从那里获取该地址?或者控件vm是否有依赖属性将字符串推送到主窗体vm中 用于用户控制的XAML 这将绑定到控件的vm

我试图实现一组控件,这些控件将由应用程序中的各种仪器包装器/适配器使用。这些控件的目标是提供所有连接设备都会使用的功能,例如验证IP地址,而不是在添加新设备支持时让每个开发人员编写自己的。我们也在考虑将WPF和MVVM作为重新分解的一部分。我很难理解的问题是,来自主窗口的vm如何与控件的vm共享本例中验证的IP地址的数据。验证IP地址后,控制vm是否会将其写回模型,并且主窗体vm会从那里获取该地址?或者控件vm是否有依赖属性将字符串推送到主窗体vm中

用于用户控制的XAML 这将绑定到控件的vm

    <Label Content="IP Address" HorizontalAlignment="Left" VerticalAlignment="Top" Width="78"/>
    <TextBox Text="{Binding Path=Part1, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="44" RenderTransformOrigin="4.427,0.626" HorizontalContentAlignment="Center"/>
    <TextBox Text="{Binding Path=Part2, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="44" RenderTransformOrigin="4.427,0.626" HorizontalContentAlignment="Center"/>
    <TextBox Text="{Binding Path=Part3, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="44" RenderTransformOrigin="4.427,0.626" HorizontalContentAlignment="Center"/>
    <TextBox Text="{Binding Path=Part4, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="44" RenderTransformOrigin="4.427,0.626" HorizontalContentAlignment="Center"/>
    <Button Command="{Binding ValidateCommand}" Content="Validate" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" />
    <TextBox Text="{Binding Result}" TextWrapping="Wrap" Width="179" Margin="0,0,0,63.6"/>
以及用户控制按钮的命令类

    private readonly IPAddressViewModel _vm;
    public IPAddressValidate(IPAddressViewModel vm)
    {
        _vm = vm;
    }

    public bool CanExecute(object parameter)
    {
        // ...... 
    }

    public event EventHandler CanExecuteChanged
    {
        // ...... 
    }

    public void Execute(object parameter)
    {
        _vm.Validates();
    }
控件按预期工作,并在输入数据后启用“验证”按钮,然后单击按钮对其进行验证。到目前为止还不错

然后我尝试以这样的形式使用这个控件

    <Button Command="{Binding ConnectCommand}" Content="Connect" HorizontalAlignment="Left" Margin="10,151,0,0" VerticalAlignment="Top" Width="75" Height="22"/>
    <TextBox Text="{Binding Result}" HorizontalAlignment="Left" Height="23" Margin="124,151,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="384"/>
    <Controls:IPAddress HorizontalAlignment="Left" Margin="10,85,0,0" VerticalAlignment="Top" Height="26" Width="498"/>
主窗体代码隐藏

public partial class IPAddress : UserControl
{
    private IPAddressViewModel _vm;

    public IPAddress()
    {
        InitializeComponent();

        //Create an insatnce of the view model and set it as the data context for this form
        _vm = new IPAddressViewModel();
        this.DataContext = _vm;
    }
}
public partial class MainWindow : Window
{
    private AdapterViewModel _vm;

    public MainWindow()
    {
        InitializeComponent();

        //Create an insatnce of the view model and set it as the data context for this form
        _vm = new AdapterViewModel();
        this.DataContext = _vm;
    }
}
我看不出如何从您的示例中实现

ParameterViewModel viewModel = new ParameterViewModel();
viewModel.OnParameterChange += ParameterViewModel_OnParameterChange;
这是否会创建另一个用户控件vm实例,而该实例不是视图使用的实例?

您可能需要按照我对重复问题的回答中的链接来获取更多相关信息。这个想法很简单。在UserControl中定义一个委托,在MainWindow.xaml.cs中为其附加一个处理程序,并在希望将数据传递到MainWindow.xaml.cs时从UserControl调用该委托。请按照提供的所有链接解释一切,而不是在这里进一步提问。
ParameterViewModel viewModel = new ParameterViewModel();
viewModel.OnParameterChange += ParameterViewModel_OnParameterChange;