3

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

4

1 回答 1

4

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

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)将两个新Window的 s 添加到您的项目中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>

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

于 2013-05-26T14:28:38.197 回答