我有两个窗户;主窗口和子窗口。我需要更新子窗口中文本块中的一些文本。但是文本不是更新的,当我在主窗口上放置该文本块时,它将更新文本。
我尝试了以下代码:
主窗口QPlayer.XAML(活动窗口)
<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" />
信息窗口AyahWindow.xaml(非活动窗口)
<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(无效)
public InfoWindow()
{
InitializeComponent();
}
public string txt;
public InfoWindow(string txt)
{
InitializeComponent();
TxtToShow.Text = txt;
}
ViewModel类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;
}
}
}
请帮助,我真的被困在这一点。
这是我的项目图片,可以更好地理解 enter image description here
答案 0 :(得分:1)
并且永远不要在视图模型中处理视图控件或类。从代码背后的按钮的事件处理程序中创建并显示InfoWindow
。
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:
查看模型:
class ProjViewModel : INotifyPropertyChanged
{
public ProjViewModel()
{
}
private string _texttoshow;
public string TextToShow
{
get { return _txttoshow; }
set { _txttoshow= value; OnPropertyChanged("TxtToShow"); }
}
}