WPF从不同窗口更新文本块

问题描述 投票:0回答:1

我有一个名为RolledPaper.xaml的窗口,其中包含一个名为SequenceValue的文本块。 SequenceValue通过键入名为SequenceRequested的文本框在另一个名为CounterSettings.xaml的窗口中定义。我希望SequenceValue始终与SequenceRequested合作。我尝试并且无法为两个窗口使用相同的datacontext。这是RolledPaper.xaml的代码:

<Window x:Class="Numbering3.View.RolledPaper"
            WindowStyle="None"
    ResizeMode="NoResize"
    BorderBrush="LightGray"
    BorderThickness="5"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Numbering3.View"
    xmlns:vm="clr-namespace:Numbering3.ViewModel"
    xmlns:cv="clr-namespace:Numbering3.Helper"
    xmlns:vo="clr-namespace:Numbering3.ViewModel"
     xmlns:sys="clr-namespace:System;assembly=mscorlib"
    mc:Ignorable="d"
    Title="RolledPaper"  Height="750" Width="1218"
    Background="{DynamicResource WindowBrush}"
    DataContextChanged="Rolledpaper_DataContextChanged">
<Window.DataContext>
    <vm:ViewModelRolledPaper/>
</Window.DataContext>


 <TextBlock x:Name="SequenceValue" Grid.Column="3" HorizontalAlignment="Left" Height="19" Margin="72,310,0,0" TextWrapping="Wrap" 
               Background="White" VerticalAlignment="Top" Width="98" Text="{Binding _SequenceValueToShow.SequenceValuetoShow, Mode=TwoWay, FallbackValue=NNN, TargetNullValue=NNN, UpdateSourceTrigger=PropertyChanged}"/>


</Grid>

代码背后:

    public partial class RolledPaper : Window
{
    public RolledPaper()
    {
        WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
        SaveKeeper.fromMain = false;
        InitializeComponent();
        this.MouseLeftButtonDown += delegate { this.DragMove(); };
        DataContextChanged += new DependencyPropertyChangedEventHandler(Rolledpaper_DataContextChanged);
    }

CounterSetting窗口:

<Window x:Class="Numbering3.View.CounterSettings"

... ...

    <Window.DataContext>
    <ViewModelCounterSettings/>
</Window.DataContext>
    <Grid Margin="0,-19,-0.4,0">
<TextBox  x:Name="SequenceRequested"  PreviewTextInput="SequenceValidationTextBox" MaxLength="10" HorizontalAlignment="Left" Height="23" Margin="154,324,0,0" TextWrapping="Wrap" 
              Text="{Binding Path=_SequenceValueToShow.SequenceValueToKeep,   FallbackValue='NNN', TargetNullValue ='NNN', Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }"  VerticalAlignment="Top" Width="120" TextChanged="SequenceRequested_TextChanged" />
    </Grid>

它的代码背后:

 public partial class CounterSettings : Window
{
    public CounterSettings()
    {
        InitializeComponent();
        this.MouseLeftButtonDown += delegate { this.DragMove(); };

        DataContextChanged += new DependencyPropertyChangedEventHandler(CounterSettings_DataContextChanged);

    }

和SequeneValue类:

   public class SequenceValue : INotifyPropertyChanged
{
        public string SequenceValueToKeep
    {
        get
        {
            return _sequenceValueToKeep=_sequenceProcessor.GetSequence();
        }

        set
        {
            if (_sequenceValueToKeep != value)
            {
                __sequenceValueToKeep = value;
                RaisePropertyChanged("SequenceValueToKeep");
            }
        }
    }
        private void RaisePropertyChanged(string prop)
    {
        if (PropertyChanged != null)
        { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}
}

我需要在counteretting => string的文本框中放置一个值Sequencevalue.SequenceValueToKeep更新=> RolledPaper窗口中的textblock显示Sequencevalue.SequenceValueToKeep。

谢谢。

c# wpf mvvm data-binding datacontext
1个回答
0
投票

解决方案1:

您可以使用一个静态ViewModel来让彼此了解。

在App.xaml中

<Application.Resources>
    ...
    <MainViewModel x:Key="mainViewModel" />
</Application.Resources>

然后在CounterSetting.xaml中

<Window otherProperties=...
        DataContext="{Binding ViewModelCounterSettings, Source={StaticResource mainViewModel}}"/>

在RolledPaper.xaml中

<Window otherProperties=...
        DataContext="{Binding ViewModelRolledPaper, Source={StaticResource mainViewModel}}"/>

MainViewModel会在您的两个ViewModel中执行,并使它们可以作为属性访问,并且还提供相同的SequenceValue实例

解决方案2:

创建SequenceValue的对象作为静态资源,如

<Application.Resources>
    ...
    <SequenceValue x:Key="sequenceValue"/>
</Application.Resources>

并将其设置为TextBox和TextBlock的DataContext

<TextBox DataContext="{StaticResource sequenceValue}" 
         Text="{Binding SequenceValueToKeep, FallbackValue='NNN', TargetNullValue='NNN', Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }"
        ... />

<TextBlock DataContext="{StaticResource sequenceValue}" 
           Text="{Binding SequenceValuetoShow, FallbackValue=NNN, TargetNullValue=NNN, UpdateSourceTrigger=PropertyChanged}"
© www.soinside.com 2019 - 2024. All rights reserved.