如何在 wpf 上的两个不同窗口中绑定两个控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16759500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:52:19  来源:igfitidea点击:

How can I bind two controls in two different windows on wpf?

c#wpfwpf-controls

提问by user2419978

I want to bind two controls like font size slider and text box, and each control is on different window on wpf, so how can I bind them?

我想绑定字体大小滑块和文本框等两个控件,并且每个控件在wpf上的不同窗口中,那么如何绑定它们?

回答by hattenn

Here's an example of how to do it:

以下是如何执行此操作的示例:

1)Create a WPF project.

1)创建一个WPF项目。

2)Change the contents of the MainWindow.xamlto the following (don't forget to correct the namespaces in all the code that I'm posting, for example in my code the namespace is WpfApplication2):

2)将 的内容更改为MainWindow.xaml以下内容(不要忘记更正我发布的所有代码中的命名空间,例如在我的代码中命名空间是WpfApplication2):

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <Button Content="Settings Window" Click="SettingsWindowButton_OnClick"/>
        <Button Content="Bound Window" Click="BoundWindowButton_OnClick"/>
    </StackPanel>
</Window>

3)Change the contents of the MainWindow.xaml.csto the following:

3)将里面的内容改成MainWindow.xaml.cs如下:

namespace WpfApplication2
{
    using System.Windows;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ViewModel viewModel = new ViewModel();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void SettingsWindowButton_OnClick(object sender, RoutedEventArgs e)
        {
            var settingsWindow = new SettingsWindow();
            settingsWindow.DataContext = viewModel;
            settingsWindow.Show();
        }

        private void BoundWindowButton_OnClick(object sender, RoutedEventArgs e)
        {
            var boundWindow = new BoundWindow();
            boundWindow.DataContext = viewModel;
            boundWindow.Show();
        }
    }
}

4)Create a class named ViewModelin your project with the following code:

4)ViewModel使用以下代码在您的项目中创建一个名为的类:

namespace WpfApplication2
{
    using System.ComponentModel;

    public class ViewModel : INotifyPropertyChanged
    {
        private int _fontSizeSetting = 10;
        public event PropertyChangedEventHandler PropertyChanged;

        public int FontSizeSetting
        {
            get { return _fontSizeSetting; }
            set
            {
                _fontSizeSetting = value;
                OnPropertyChanged("FontSizeSetting");
            }
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

5)Add two new Windows to your project named BoundWindowand SettingsWindowwith the following markup:

5)Window向您的项目中添加两个新的s,BoundWindowSettingsWindow使用以下标记:

<Window x:Class="WpfApplication2.BoundWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="BoundWindow" Height="300" Width="300">
    <Grid>
        <TextBox FontSize="{Binding FontSizeSetting, Mode=TwoWay}" Text="test..."/>
    </Grid>
</Window>

-

——

<Window x:Class="WpfApplication2.SettingsWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="SettingsWindow" Height="300" Width="300">
    <Grid>
        <Slider Value="{Binding FontSizeSetting, Mode=TwoWay}" Minimum="10" Maximum="100"/>
    </Grid>
</Window>

Now everything should be working as expected. What you basically did was to create a view model to set as the DataContextof your Windows. They both bind to the FontSizeSettingproperty of your view model, and when you change it in one window, WPF binding system takes care of changing the other value automatically.

现在一切都应该按预期工作。您基本上所做的是创建一个视图模型以设置为DataContext您的Windows 的。它们都绑定到FontSizeSetting您的视图模型的属性,当您在一个窗口中更改它时,WPF 绑定系统会自动更改另一个值。