c# - 从第二个 lib 项目绑定(bind)用户控件不起作用 - wpf mvvm

标签 c# wpf xaml mvvm

我的任务:我想绑定(bind)文本框和按钮。

尽管我发现了很多关于它的主题,但我无法解决我的问题。
我有项目:带有 WPF 应用程序的客户端 使用默认 XAML 无绑定(bind) ,它从作为库的 MenuWindow 项目中获取上下文。在 MenuWindow 项目中,我有名为:MenuProgram 的用户控件 WPF。

<UserControl x:Class="MenuWindow.MenuProgram"
         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" 
         xmlns:local="clr-namespace:MenuWindow"
         mc:Ignorable="d" 
         d:DesignHeight="550" d:DesignWidth="780">
<UserControl.DataContext>
    <local:MenuViewModel/>
</UserControl.DataContext>
<Grid Background="#FF6F6FA4">
    <Label x:Name="lblTitle" Content="GUI Export Revit Data" HorizontalAlignment="Left" Margin="277,31,0,0" VerticalAlignment="Top" Height="50" Width="258" FontSize="24" FontWeight="Bold"/>
    <Label x:Name="lblPrtdPath" Content="File prtd path" HorizontalAlignment="Left" Margin="200,176,0,0" VerticalAlignment="Top"/>
    <Label x:Name="lblXmlPath1" Content="File xml path1" HorizontalAlignment="Left" Margin="200,222,0,0" VerticalAlignment="Top"/>
    <Label x:Name="lblXmlPath2" Content="File xml path2" HorizontalAlignment="Left" Margin="200,266,0,0" VerticalAlignment="Top"/>
    <TextBox x:Name="tbxPrtd" HorizontalAlignment="Left" Height="23" Margin="302,176,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="268" Text="{Binding PrtdFilePath}"/>
    <TextBox x:Name="tbxXml1" HorizontalAlignment="Left" Height="23" Margin="302,222,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="268" Text="{Binding XmlFilePath1}"/>
    <TextBox x:Name="tbxXml2" HorizontalAlignment="Left" Height="23" Margin="302,266,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="268" Text="{Binding XmlFilePath2}"/>
    <Button x:Name="SayHi" Content="Start" HorizontalAlignment="Left" Margin="302,450,0,0" VerticalAlignment="Top" Width="174" Height="84" FontSize="22" Command="{Binding SayHi}" />
    <Button x:Name="btnAbout" Content="About" HorizontalAlignment="Left" Margin="705,496,0,0" VerticalAlignment="Top" Width="55" Height="38" Command="{Binding SayHi}"/>
</Grid>

所以我有
<UserControl.DataContext>
<mv:MenuViewModel/>
</UserControl.DataContext>

并使用文本框或按钮我想使用绑定(bind)。

在这个用户控件的代码后面,除了默认初始化之外什么都没有。
在项目菜单中有:
带有映射的 MenuArguments.cs:
public string PrtdFilePath { get; set; }
public string XmlFilePath1 { get; set; }
public string XmlFilePath2 { get; set; }

中继命令:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace MenuWindow
{
public class RelayCommand : ICommand
{
    private readonly Func<Boolean> _canExecute;
    private readonly Action _execute;

    public RelayCommand(Action execute)
        : this(execute, null)
    {
    }

    public RelayCommand(Action execute, Func<Boolean> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");
        _execute = execute;
        _canExecute = canExecute;
    }

    public event EventHandler CanExecuteChanged
    {
        add
        {
            if (_canExecute != null)
                CommandManager.RequerySuggested += value;
        }
        remove
        {
            if (_canExecute != null)
                CommandManager.RequerySuggested -= value;
        }
    }

    public Boolean CanExecute(Object parameter)
    {
        return _canExecute == null ? true : _canExecute();
    }

    public void Execute(Object parameter)
    {
        _execute();
    }
}
}

和 MenuViewModel.cs
namespace MenuWindow
{
public class MenuViewModel : INotifyPropertyChanged
{
    public string gowno;
    public MenuArguments _menuArgumenty;
    public string PrtdFilePath
    {
        get { return _menuArgumenty.PrtdFilePath; }
        set
        {
            _menuArgumenty.PrtdFilePath = value;
            OnPropertyChanged("PrtdFilePath");
        }
    }
    public string XmlFilePath1
    {
        get { return _menuArgumenty.XmlFilePath1; }
        set
        {
            _menuArgumenty.XmlFilePath1 = value;
            OnPropertyChanged("XmlFilePath1");
        }
    }
    public string XmlFilePath2
    {
        get { return _menuArgumenty.XmlFilePath2; }
        set
        {
            _menuArgumenty.XmlFilePath2 = value;
            OnPropertyChanged("XmlFilePath2");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public MenuViewModel()
    {
        _menuArgumenty = new MenuArguments();
    }
    public ICommand SayHi
    {
        get
        {
            return new RelayCommand(SayHiExcute, CanSayHiExcute);
        }
    }

    private void SayHiExcute()
    {
        if (!MenuArgumentsExists(_menuArgumenty))
        {
            MessageBox.Show(string.Format("Hi {0} {1}!", _menuArgumenty.PrtdFilePath, _menuArgumenty.XmlFilePath1));
            SavePerosn(_menuArgumenty);
        }
        else
        {
            MessageBox.Show(string.Format("Hey {0} {1}, you exists in our database!", _menuArgumenty.PrtdFilePath, _menuArgumenty.XmlFilePath1));
        }
    }

    private void SavePerosn(MenuArguments _menuArgumenty)
    {
        //Some Database Logic
    }

    private bool CanSayHiExcute()
    {
        return !MenuArgumentsExists(_menuArgumenty);
    }

    private bool MenuArgumentsExists(MenuArguments _menuArgumenty)
    {
        //Some logic
        return false;
    }

}
}

当我启动程序调试器时,会检查绑定(bind)属性。窗口出现后,绑定(bind)没有反应。我做错了什么?请帮我。

BR,
学生塞纳留斯

感谢您的评论,您的评论的答案:

@虎斑 - 我想绑定(bind)文本框:PrtdFilePath、XmlFilePath1、XmlFilePath1 和按钮 SayHi

@maulik堪萨拉 - 你是对的,我正在尝试其他一些方法,但我没有删除代码。它应该是只有本地的版本。

@grek40 - 我的示例适用于 Window 解决方案中的一个项目,而不适用于在另一个项目中设置的 UserControl。这是图片:
enter image description here

@mm8 - 我希望通过将数据放入文本框或单击按钮来查看断点:
        public string PrtdFilePath
    {
        get { return _menuArgumenty.PrtdFilePath; }
        set
        {
            _menuArgumenty.PrtdFilePath = value;
            OnPropertyChanged("PrtdFilePath");
        }
    }

最后,我认为 XAML 中的代码有问题。我正在阅读有关查找绑定(bind)/ View 模型/路径的父子关系,但我很困惑,我不知道如何解决它。请帮助我,感谢您的所有评论。

@grek40 这里是 Main APP WPF 中的代码,我从 MenuWindow 添加上下文。
此 MainWindow WPF APP 具有默认 XAML。
public MainWindow()
    {
        InitializeComponent();
        menuProgram = new MenuProgram();//User Control
        sw = new SharedWindow();//WPF window
        this.Close();
        sw.Content = menuProgram.Content;// here I set context
        sw.ShowDialog();
    }

和 XAML:
<Window x:Class="Client.MainWindow"
    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"
    xmlns:local="clr-namespace:Client"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>

</Grid>
</Window>

您更改的整个代码:
    public partial class MainWindow : Window
{
    private SharedWindow sw;
    private MenuProgram menuProgram;

    public MainWindow()
    {
        InitializeComponent();
        menuProgram = new MenuProgram();
        SetForContext();
    }
    private void SetForContext()
    {
        sw = new SharedWindow();
        this.Close();
        sw.Content = menuProgram;
        sw.ShowDialog();
    }

最佳答案

您需要设置 UserControl作为窗口Content ,而不是 ContentUserControl :

sw.Content = menuProgram;// here I set context
/* Bad: sw.Content = menuProgram.Content; */

您的 DataContext分配给 UserControl本身,所以如果你移动 Content树到不同的 Parent ,它将不再有它的旧 DataContext .

关于c# - 从第二个 lib 项目绑定(bind)用户控件不起作用 - wpf mvvm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48142513/

相关文章:

c# - 使用 LightInject 自动注册依赖项

c# - 如何使用 HTML unicode 转义来停止 ASP.NET Core 2 MVC 的 Razor 引擎?

c# - 在 WPF 表单上显示 HTML 片段

c# - 自定义控件中的 WPF 数据绑定(bind)

WPF MVVM 数据绑定(bind) - 检查空间,然后清除复选框

.net - WPF功能区,选择功能区选项卡时更改主要内容

c# - 为什么 Parallel.ForEach 比顺序循环慢两倍

c# - 将文件名保存到sql数据库并同时运行ssis包

c# - 按下按钮上的选项卡时更改按钮上的焦点颜色

c# - GridViewColumn 内容和 VerticalAlignment