【发布时间】:2014-07-30 01:29:08
【问题描述】:
我的应用程序中有两个UserControls,它们都驻留在MainWindow 上。现在我需要在这两者之间进行交互,这样当我点击UserControl1 中的Button 时,TextBlock 中TextBlock 的Text 属性会发生变化。
在没有 MVVM 的情况下如何实现这一点?我很想看到 MVVM 解决方案,但如果它是完整的,因为我对它完全陌生,而且它非常压倒性。
主窗口:
<Window x:Class="WpfApplication23.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:wpfApplication23="clr-namespace:WpfApplication23">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<wpfApplication23:UserControl1/>
<wpfApplication23:UserControl2 Grid.Row="1"/>
</Grid>
</Window>
用户控制1:
<UserControl x:Class="WpfApplication23.UserControl1"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Grid>
<Button Content="Change Text"
Width="200"
Height="80"
Click="ButtonBase_OnClick"/>
</Grid>
</UserControl>
用户控制2:
<UserControl x:Class="WpfApplication23.UserControl2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TextBox Width="100" Height="20"/>
</Grid>
</UserControl>
按钮的点击事件:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
// Access the TextBlock in UserControl2 and Change its Text to "Hello World"
}
【问题讨论】:
标签: c# wpf xaml user-controls