简体   繁体   English

textBlock 未更新(RaisePropertyChanged 不起作用?)

[英]textBlock isn't updated (RaisePropertyChanged isn't working?)

My program has two windows.我的程序有两个 windows。 The content of TextBox on MainWindow should change the content of a TextBlock on CalcWindow. MainWindow 上 TextBox 的内容应该改变 CalcWindow 上 TextBlock 的内容。 However, the TextBlock doesn't change, even after the TextBox is changed.但是,即使在 TextBox 更改后,TextBlock 也不会更改。

I added "RaisePropertyChanged" to UISimpleData.我将“RaisePropertyChanged”添加到 UISimpleData。 So, the content of TextBox is correctly changed.因此,TextBox 的内容已正确更改。 But, it doesn't change the TextBlock on CalcWindow.但是,它不会更改 CalcWindow 上的 TextBlock。

MainWindow.xaml主窗口.xaml

<Window x:Class="DoubleToTextBlockBinding.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="DataUpdate" Height="220.276" Width="400">

    <Grid Height="190" Margin="0,0,-0.667,0" VerticalAlignment="Top">
        <Label Content="Target Value" HorizontalAlignment="Right" Margin="0,0,112,142" VerticalAlignment="Bottom" Width="78"/>
        <TextBox Margin="0,0,24,142" HorizontalAlignment="Right" VerticalAlignment="Bottom" Height="22" Width="60"
                 Text="{Binding DoubleField, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}">
            <TextBox.Style>
                <Style TargetType="{x:Type TextBox}">
                    <Style.Triggers>
                        <Trigger Property="Validation.HasError" Value="true">
                            <Setter Property="ToolTip" Value="{Binding (Validation.Errors)[0].ErrorContent, 
                                RelativeSource={RelativeSource Self}}"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>
        <TextBlock Text="{Binding DoubleField}" HorizontalAlignment="Right" VerticalAlignment="Bottom" 
                   Margin="0,0,25,115" Height="22" Width="60"/>
        <Button Name="ConfirmButton" Content="Confirm" Margin="85,0,25.666,58" HorizontalAlignment="Right" VerticalAlignment="Bottom" Click="ConfirmButton_Click"/>
    </Grid>
</Window>

MainWindow.xaml.cs主窗口.xaml.cs

using DoubleToTextBlockBinding.ViewModels;
using DoubleToTextBlockBinding.Views;
using System.Windows;

namespace DoubleToTextBlockBinding
{
    public partial class MainWindow : Window
    {
        private UISimpleData _uiData = new UISimpleData();

        public MainWindow()
        {
            InitializeComponent();
            DataContext = _uiData;
        }

        private void ConfirmButton_Click(object sender, RoutedEventArgs e)
        {
            new CalcWindow().Show();
        }
    }
}

App.xaml.cs App.xaml.cs

using System.Windows;

namespace DoubleToTextBlockBinding
{
    public partial class App : Application
    {
        public App()
        {
            System.Windows.FrameworkCompatibilityPreferences
                       .KeepTextBoxDisplaySynchronizedWithTextProperty = false;
        }
    }
}

Views/CalcWindow.xaml视图/CalcWindow.xaml

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viewModels="clr-namespace:DoubleToTextBlockBinding.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        x:Class="DoubleToTextBlockBinding.Views.CalcWindow"
        Title="Bound Window" Width="400" Height="212">
    <Grid>
        <TextBlock Text="{Binding DoubleField}" x:Name="textBox"  Width="104"
            Margin="148,84,0,0" HorizontalAlignment="Left" VerticalAlignment="Top">
            <TextBlock.DataContext>
                <viewModels:UISimpleData/>
            </TextBlock.DataContext>
        </TextBlock>
    </Grid>
</Window>

Views/CalcWindow.xaml.cs视图/CalcWindow.xaml.cs

using System.Windows;
using DoubleToTextBlockBinding.ViewModels;

namespace DoubleToTextBlockBinding.Views
{
    public partial class CalcWindow : Window
    {
        private UISimpleData _uiData = new UISimpleData();
        public CalcWindow()
        {
            InitializeComponent();
            this.DataContext = _uiData;
        }
    }
}

ViewModels/UISimpleData.cs ViewModels/UISimpleData.cs

using System;
using System.ComponentModel;
using System.Windows;

namespace DoubleToTextBlockBinding.ViewModels
{
    public class UISimpleData : INotifyPropertyChanged, IDataErrorInfo
    {
        private double _doubleField = 2.0;

        public double DoubleField
        {
            get
            {
                return _doubleField;
            }
            set
            {
                if (_doubleField == value)
                    return;

                _doubleField = value;
                RaisePropertyChanged("DoubleField");
            }
        }

        public string this[string propertyName]
        {
            get
            {
                string validationResult = null;
                switch (propertyName)
                {
                    case "DoubleField":
                        {
                            if (DoubleField < 0 || DoubleField > 5)
                                validationResult = "DoubleField is out of range";
                            break;
                        }
                    default:
                        throw new ApplicationException("Unknown Property being validated on UIData");
                }
                return validationResult;
            }
        }

        public string Error { get { return "Not Implemented"; } }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string property)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
            //MessageBox.Show("Changed to " + DoubleField);
        }
    }
}

Expected behavior:预期行为:

  1. Change the content of TextBox to "3".将 TextBox 的内容更改为“3”。

(You will see the TextBlock on the same window changes to "3".) (您将看到同一 window 上的 TextBlock 更改为“3”。)

  1. Click on the "Confirm" button.单击“确认”按钮。

  2. A new window appears.出现新的 window。 Check if the content of the TextBlock is "3".检查 TextBlock 的内容是否为“3”。

(The actual result becomes "2" here.) (实际结果在此处变为“2”。)

... This is the best I can do for now. ......这是我现在能做的最好的。 Please help me.请帮我。 Thank you.谢谢你。

you are creating new instances of your viewmodel in both classes.您正在两个类中创建视图模型的新实例。 If you change something in the viewmodel in MainWindow, you have to pass this model to the CalcWindow.如果您在 MainWindow 的视图模型中更改某些内容,则必须将此 model 传递给 CalcWindow。

my guess is that you should write the constructor for CalcWindow like this:我的猜测是你应该像这样为 CalcWindow 编写构造函数:

public CalcWindow(UISimpleData yourViewModelFromMainWindow)
{
    InitializeComponent();
    this.DataContext = yourViewModelFromMainWindow;
}

and in the MainWindow, in method ConfirmButton_Click在 MainWindow 的 ConfirmButton_Click 方法中

private void ConfirmButton_Click(object sender, RoutedEventArgs e)
{
    new CalcWindow(this.DataContext as UISimpleData).Show();
}

I hope this is helpful.我希望这是有帮助的。 If it's not, do not hesitate to ask.如果不是,请毫不犹豫地问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM