C# WPF中不使用MVVM的两个用户控件之间的交互

C# WPF中不使用MVVM的两个用户控件之间的交互,c#,wpf,xaml,user-controls,C#,Wpf,Xaml,User Controls,我的应用程序中有两个用户控件,它们都位于主窗口。现在我需要在这两者之间进行交互,这样当我在UserControl1中单击按钮时,UserControl2中TextBlock的Text属性就会改变 没有MVVM我如何实现这一点?我很想看到MVVM解决方案,但如果它是完整的,因为我是全新的,它是非常压倒性的 主窗口: <Window x:Class="WpfApplication23.MainWindow" xmlns="http://schemas.microsoft.com

我的应用程序中有两个
用户控件
,它们都位于
主窗口
。现在我需要在这两者之间进行交互,这样当我在
UserControl1
中单击
按钮时,
UserControl2
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> 
<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>
<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"
}
要在没有MVVM的情况下执行此操作,您需要执行以下操作:

  • 在第一个用户控件上设置类似“UpdateText”的事件,让按钮的click方法引发此事件

    public event Action<string> UpdateText;
    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
       // Access the TextBlock in UserControl2 and Change its Text to "Hello World"
       if (UpdateText != null)
           UpdateText("HelloWorld");
    }
    
  • 现在,正确的答案是使用MVVM,但是所需的代码样本对于StackOverflow来说太长了。但是,我将提供以下步骤:

  • 在第二个用户控件上为“Text”属性设置一个
    dependencProperty
    ,并绑定到它:

    <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" Text="{Binding ControlText}"/>
        </Grid>
    </UserControl>
    
    
    
  • 在单击按钮时将调用的第一个用户控件上放置一个
    ICommand
    依赖项属性。在
    main窗口
    上绑定一个函数,该函数将
    ControlText
    属性设置为参数对象


  • 可能不是最好的“第一个MVVM”示例,因为它需要一些高级概念(命令和依赖属性),但它应该不会太难实现。

    我不会为您编写程序,但如果您想了解MVVM方面的说明,我很乐意回答您可能有的任何问题。非常感谢Bradley,如何在主窗口中收听事件?感谢Bradley,这是我最关心的问题,如何在
    myUserControl2
    中获取文本块?经过数小时的搜索,我想我终于接近一个解决方案了。@Vahid您可以调用第二个用户控件的
    SetText
    方法,然后设置文本块的text属性
    MainWindow
    没有,也不应该访问
    UserControl2
    @Vahid的内部控制。没错,MVVM会让它更干净(使用它的原因之一)。我想不出还有什么别的办法来清理它。您提到MVVM对您来说是压倒性的;我很乐意在一个小时内和你谈谈这件事,也许我可以澄清一些困惑。
    <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" Text="{Binding ControlText}"/>
        </Grid>
    </UserControl>