WPF如何从其他页面更改属性? [英] WPF How change a property in a page from another?

查看:199
本文介绍了WPF如何从其他页面更改属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变属性可见在PAGE1控制的,当我点击的PAGE2一个按钮。在第2页是与用于用户的消息的Pop'Up窗口。我想,当他点击下方的按钮,该财产在第2页的控制(在同一时间仍然显示在背景中)的变化可见。

I would like to change the property Visible of a control in the PAGE1 when I click on a button on the PAGE2. The PAGE2 is a Pop'Up window with a message for the user. I want when he clicks on a button below that the property Visible from a control in the PAGE2 (still displayed in the background at the same time) change.

有关问题我的是,我的按钮的事件属于PAGE2类的,我不能够从那里达到PAGE1的对象。

The problem for me is that the event of my button belongs to the PAGE2 class and I'm not able to reach PAGE1's objects from there.

推荐答案

我要做到这一点,你应该用一个中间对象共享某些数据的正确的方式。

I you want to do it the "right" way you should share some data with an intermediate object.

下面是一个完整的示例:

Here is a full sample:

查看模型,由两页/ Windows的共享:

The view-model, shared by both pages/windows:

using System.ComponentModel;

namespace WpfMagic
{
    public class MyViewModel : INotifyPropertyChanged
    {
        private bool flag;
        public bool Flag
        {
            get { return flag; }
            set
            {
                if (value != flag)
                {
                    flag = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("Flag"));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };
    }
}



主页/窗口:

The main page/window:

XAML:

<Window x:Class="WpfMagic.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfMagic"
        Title="MainWindow"
        SizeToContent="WidthAndHeight">
    <StackPanel>
        <StackPanel.Resources>
            <BooleanToVisibilityConverter x:Key="boolToVisibility"></BooleanToVisibilityConverter>
        </StackPanel.Resources>
        <TextBlock Visibility="{Binding Model.Flag,Converter={StaticResource boolToVisibility}}">Hey I'm Here!</TextBlock>
        <Button Click="Button_Click">Show Popup!</Button>
    </StackPanel>
</Window>



代码隐藏:

Code-behind:

using System.Windows;

namespace WpfMagic
{
    public partial class MainWindow : Window
    {
        public MyViewModel Model { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            Model = new MyViewModel();

            this.DataContext = this;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            new AnotherWindow(Model).Show();
        }
    }
}



在弹出页/窗口:

The popup page/window:

XAML:

<Window x:Class="WpfMagic.AnotherWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="AnotherWindow" Height="300" Width="300">
    <Grid>
        <CheckBox IsChecked="{Binding Model.Flag}">Check Me!</CheckBox>
    </Grid>
</Window>



代码隐藏:

Code-behind:

using System.Windows;

namespace WpfMagic
{
    public partial class AnotherWindow : Window
    {
        public MyViewModel Model { get; set; }

        public AnotherWindow(MyViewModel model)
        {
            InitializeComponent();

            Model = model;

            this.DataContext = this;
        }
    }
}

如果你得到这个例子中,你会得到90%的 MVVM

If you get this example you'll get 90% of MVVM.

这篇关于WPF如何从其他页面更改属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆