【问题标题】:Interaction between two usercontrols in WPF without using MVVMWPF中两个用户控件之间的交互而不使用MVVM
【发布时间】:2014-07-30 01:29:08
【问题描述】:

我的应用程序中有两个UserControls,它们都驻留在MainWindow 上。现在我需要在这两者之间进行交互,这样当我点击UserControl1 中的Button 时,TextBlockTextBlockText 属性会发生变化。

在没有 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


    【解决方案1】:

    要做到这一点没有 MVVM,您需要执行以下操作:

    1. 在第一个用户控件上设置一个类似“UpdateText”的事件,让按钮的点击方法引发这个事件。

      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");
      }
      
    2. 监听MainWindow 中的事件,然后调用第二个用户控件上的函数来更新文本块。比如:

      public MainWindow()
      {
          InitializeComponent();
      
          myUserControl1.UpdateText += HandleUpdateText;
      }
      
      private void HandleUpdateText(String newText)
      {
          myUserControl2.SetText(newText);
      }
      

    现在正确的答案是使用 MVVM,但所需的代码示例对于 StackOverflow 来说太长了。不过我会提供步骤:

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

      <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>
      
    2. ICommand 依赖属性放在第一个将在按钮单击时调用的用户控件上。在MainWindow 上绑定一个函数,将ControlText 属性设置为参数对象。

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

    【讨论】:

    • 我不会为你编写程序,但如果你想了解 MVVM 方面的问题,我很乐意回答你的任何问题。
    • 非常感谢 Bradley,我怎样才能在 MainWindow 中收听 Event?
    • 感谢 Bradley,这是我最关心的问题,我如何在 myUserControl2 中获取 TextBlock?经过数小时的搜索,我想我终于接近解决方案了。
    • @Vahid 您调用第二个用户控件的SetText 方法,然后设置文本块的文本属性。 MainWindow 没有也不应该访问UserControl2 的内部控制。
    • @Vahid 没错,MVVM 会让它更干净(使用它的原因之一)。我想不出有什么办法可以用另一种方法来清理它。您提到 MVVM 对您来说是压倒性的;我很乐意在大约一个小时后与您聊聊,也许我可以解开一些困惑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    相关资源
    最近更新 更多