C# 在另一个WPF窗口的标签上显示按钮的内容

C# 在另一个WPF窗口的标签上显示按钮的内容,c#,wpf,data-binding,C#,Wpf,Data Binding,我有一个按钮,包含从查询返回的数据计数; 单击该按钮后,将打开一个不同的WPF表单(存储在变量中,从不关闭)。我希望表单上的按钮和文本块包含相同的数据 我对数据绑定感到困惑;因为所有的教程都只在同一个表单上解释数据绑定 用鼠标点击按钮 <Button x:Name="CounterBtn" ToolTip="Open Teller" Content="0" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight=

我有一个按钮,包含从查询返回的数据计数; 单击该按钮后,将打开一个不同的WPF表单(存储在变量中,从不关闭)。我希望表单上的按钮和文本块包含相同的数据

我对数据绑定感到困惑;因为所有的教程都只在同一个表单上解释数据绑定

用鼠标点击按钮

<Button x:Name="CounterBtn" ToolTip="Open Teller" Content="0" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" IsTabStop="False" Foreground="{DynamicResource PrimaryHueMidBrush}" Click="CounterBtn_Click" />
打开和隐藏窗口的函数:

        private void ToggleCounterWindow() {
            if (!CWindow_IsVisible) { CounterWindow.Show(); } else { CounterWindow.Hide(); }
            CWindow_IsVisible = !CWindow_IsVisible;
            return;
        }
显示查询结果:

CounterBtn.Content = dt.Rows.Count.ToString();
柜台窗口的代码隐藏

    public partial class CounterWindow : Window, IDisposable
    {
        public CounterWindow()
        {
            InitializeComponent();
        }
}


这个问题与数据绑定没有直接关系
退一步说,您只需要在两个不同的位置引用相同的变量/值。

这个问题有很多解决方案。
想到的最简单的事情是将同一对象共享给两个ViewModels。

在本例中,您要共享的共享计数将是使用Prism MVVM框架的可绑定基本对象上的属性(为简洁起见)


现在,我们可以让两个视图模型直接引用该对象

public class MainWindowViewModel : BindableBase
{
    public QueryResults QueryResults { get; }

    public MainWindowViewModel(QueryResults queryResults)
    {
        QueryResults = queryResults;
    }

}

public class PopupViewModel : BindableBase
{
    public QueryResults QueryResults { get; }

    public PopupViewModel(QueryResults queryResults)
    {
        QueryResults = queryResults;
    }
}
因此,当显示弹出窗口时,可以将MainWindowViewModel引用提供给PopupViewModel

public class MainWindowViewModel : BindableBase
{
    public QueryResults QueryResults { get; }

    public MainWindowViewModel(QueryResults queryResults)
    {
        QueryResults = queryResults;
    }

    private DelegateCommand _showPopupCommand;
    public DelegateCommand ShowPopupCommand =>
        _showPopupCommand ?? (_showPopupCommand = new DelegateCommand(ExecuteShowPopupCommand));

    void ExecuteShowPopupCommand()
    {
        var popupViewModel = new PopupViewModel(QueryResults);
        var popupWindow = new PopupWindow { DataContext = popupViewModel };
        popupWindow.Show();
    }

}
这不是做事情的最佳方式。
我不会直接引用/共享QueryResults类。
我将把它放在共享控制器界面上,并使用容器将其注入ViewModels构造函数中。

但这是更高级的东西,虽然在生产代码中是必需的,但在您学习如何在MVVM应用程序中开发时,不需要这些东西来解决这个问题。

您已经尝试过了吗?请提供并让我们知道您有哪些特殊问题。让两个窗口共享相同的datacontext,然后它们可以绑定完全相同的数据。嗨,Kevin,您有初学者教程来解释这一点吗?请给我们一些代码。@Fimlore我们确实需要至少一个代码示例。我们需要一些细节。例如,第一个窗口是否创建了第二个窗口?
public class MainWindowViewModel : BindableBase
{
    public QueryResults QueryResults { get; }

    public MainWindowViewModel(QueryResults queryResults)
    {
        QueryResults = queryResults;
    }

}

public class PopupViewModel : BindableBase
{
    public QueryResults QueryResults { get; }

    public PopupViewModel(QueryResults queryResults)
    {
        QueryResults = queryResults;
    }
}
public class MainWindowViewModel : BindableBase
{
    public QueryResults QueryResults { get; }

    public MainWindowViewModel(QueryResults queryResults)
    {
        QueryResults = queryResults;
    }

    private DelegateCommand _showPopupCommand;
    public DelegateCommand ShowPopupCommand =>
        _showPopupCommand ?? (_showPopupCommand = new DelegateCommand(ExecuteShowPopupCommand));

    void ExecuteShowPopupCommand()
    {
        var popupViewModel = new PopupViewModel(QueryResults);
        var popupWindow = new PopupWindow { DataContext = popupViewModel };
        popupWindow.Show();
    }

}