【问题标题】:How can I bind two controls in two different windows on wpf?如何在 wpf 的两个不同窗口中绑定两个控件?
【发布时间】:2013-05-26 22:28:38
【问题描述】:

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

【问题讨论】:

  • 你有什么尝试吗?
  • 这是一个大学项目,我可以在同一个窗口上绑定,但我想知道如何在不同的窗口之间进行绑定,或者除了绑定还有其他方法吗?我需要从其他窗口的滑块控件中更改文本大小..
  • 您必须为两个窗口使用相同的 ViewModel 和 FontSize 之类的属性。将其设置为Windows的DataContext并绑定到它。

标签: c# wpf wpf-controls


【解决方案1】:

下面是一个例子:

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 添加到您的名为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>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    相关资源
    最近更新 更多