简体   繁体   中英

How to update textblock text of an inactive window from main window

I have two windows; main window and a sub window. I need to update some text in a textblock which is on sub window. But the text is not updates, when I put that textblock on main window, it updates the text.

I tried the following code:

Main Window QPlayer.XAML (Active Window)

<Window x:Class="AlQuran.Views.QPlayer"
        xmlns:ViewModel="clr-namespace:AlQuran.ViewModel"
        Width="300" Height="306" WindowStyle="None"
        ResizeMode="NoResize" BorderThickness="0" Foreground="White" AllowsTransparency="True" MouseDown="Window_MouseDown" SnapsToDevicePixels="True" Loaded="Window_Loaded">
    <Window.Background>
        <ImageBrush ImageSource="../Resources/AlQuran_Window.png"/>
    </Window.Background>
    <Window.DataContext>
        <ViewModel:QuranViewModel/>
    </Window.DataContext>
    <Grid >
                <Image x:Name="BtnPlay" Grid.Column="0" MouseDown="BtnPlay_MouseDown" HorizontalAlignment="Center" VerticalAlignment="Center" Width="79" Height="79">
                    <Image.Style>
                        <Style TargetType="{x:Type Image}">
                            <Setter Property="Source" Value="../Resources/BtnPlay.png"/>
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Source" Value="../Resources/BtnPlay_Over.png"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Image.Style>
                    <Image.InputBindings>
                        <MouseBinding Gesture="LeftClick" Command="{Binding Play_Btn, Mode=OneTime}" />
                    </Image.InputBindings>
                </Image>

        <TextBlock Grid.Column="1" Grid.Row="6" Text="{Binding TxtAyah, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Height="47" Background="#FF38ECEC" FontSize="16" />

Info Window AyahWindow.xaml (Inactive window)

<Window x:Class="AlQuran.Views.AyahWindow"
        xmlns:local="clr-namespace:AlQuran"
        xmlns:ViewModel="clr-namespace:AlQuran.ViewModel"
        Width="900" Height="630"  WindowStartupLocation="CenterScreen" WindowStyle="None"
        ResizeMode="NoResize" BorderThickness="0" Foreground="White" Opacity=".94" AllowsTransparency="True" MouseDown="Window_MouseDown" >

    <Window.Resources>
        <ViewModel:QuranViewModel x:Key="QViewModel"/>
    </Window.Resources>
    <Window.DataContext>
        <ViewModel:QuranViewModel />
    </Window.DataContext>
    <Grid >
         <TextBlock Grid.Column="1" Grid.Row="1" ScrollViewer.CanContentScroll="True"
            Text="{Binding TxtAyah, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            x:Name="AyahLine" TextWrapping="Wrap" Background="#FFF51E1E" 
            FontFamily="Futura Md BT" FontSize="36" TextAlignment="Right"/>
    </Grid>

InfoWindow AyahWindow.XAML.CS (Inactive)

        public InfoWindow()
        {
            InitializeComponent();
        }
        public string txt;
        public InfoWindow(string txt)
        {
            InitializeComponent();
            TxtToShow.Text = txt;
        }

ViewModel Class QuranViewModel.CS

namespace AlQuran.ViewModel
{
    public class QuranViewModel : INotifyPropertyChanged
    {

        public ICommand Play_Btn { get; set; }

        public QuranViewModel()
        {
            Play_Btn = new RelayCommand(PlayMethod, CanExecuteMyMethod);
        }

        private string _txtAyah;

        public string TxtAyah
        {
            get { return _txtAyah; }
            set { _txtAyah = value; OnPropertyChanged("TxtAyah"); }
        }

        private void PlayMethod(object parameter)
        {
            TxtAyah =new Random().Next(5,999999).ToString() ;

            AyahWindow aw = new AyahWindow(TxtAyah);
            aw.txt = TxtAyah;

        }

    }
}

Please help, I am really stuck at this point.

and here is my project image to get better understanding enter image description here

And never handle view controls or classes in you view model. Create and show the InfoWindow from the buttons's event handler from your code-behind. MainWindow.xaml:

<Window x:Class="MainWindow"
        DataContext="{StaticResource ProjViewModel}">
  <Grid>
      <Button Click="ShowWindow_OnClick"/>
      <TextBox Text="{Binding TextToShow, UpdateSourceTrigger=PropertyChanged}"/>
  </Grid>
</Window>

MainWindow.xaml.cs:

public partial class MainWndow
{
    private void ShowWindow_OnClick(object sender, RoutedEventArgs e)
    {
      new InfoWindow().Show();
    }
}

InfoWindow.xaml

<Window x:Class="InfoWindow" 
        DataContext="{StaticResource ProjViewModel}">
  <Grid>
        <TextBlock Text="{Binding TextToShow}"/>
    </Grid>
</Window>

App.xaml:

View model:

class ProjViewModel : INotifyPropertyChanged
{
    public ProjViewModel()
    {
    }

    private string _texttoshow;
    public string TextToShow
    {
        get { return _txttoshow; }
        set { _txttoshow= value; OnPropertyChanged("TxtToShow"); }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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