其他分享
首页 > 其他分享> > 如何在WPF的两个不同窗口中绑定两个控件?

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

作者:互联网

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

解决方法:

这是一个如何做的例子:

1)创建一个WPF项目.

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)将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)使用以下代码在您的项目中创建一个名为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)使用以下标记将两个新的Windows添加到名为BoundWindow和SettingsWindow的项目中:

<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>

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

标签:wpf-controls,wpf,c
来源: https://codeday.me/bug/20191030/1971094.html