如何在主窗口中绑定两个usercontrol(WPF / C#) [英] How to bind two usercontrol in mainwindow (WPF/C#)

查看:132
本文介绍了如何在主窗口中绑定两个usercontrol(WPF / C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Oke,我有一个UserControl,一个TextBox和一个Buton,我有第二个用户控件,同时DataGrid和底部的按钮。我想说的是当我在第一个UserControl中点击按钮时,我将文本框中的值添加到datagrid中Usercontrol,当我在第二个usercontrol中单击Datagrid行然后在第二个Usercontrol中单击按钮时,我将数据网格行中的值添加到第一个UserControl中的文本框中。我的问题是我不知道如何将其绑定到corectly工作。请帮助任何帮助是好的..



我尝试过:



我不知道该怎么做因为我是wpf的新手,这种制作Windows应用程序的方法

Oke, I have one UserControl whith one TextBox and one buton , I have second usercontrol whith DataGrid and button on the bottom.I want to acompish that when i click on button in first UserControl i add value from textbox to datagrid in second Usercontrol and when i click on Datagrid row in second usercontrol and then click to button in second Usercontrol i add value from datagrid row to the textbox in the frst UserControl.. My problem is i dont know how to bind it to work corectly . Please help any help is good ..

What I have tried:

I dont know what to do coz i am new at wpf and this way of making Windows aplication

推荐答案

这样做的一种方法是你发布一个事件用户控制并在窗口级别连接该事件,以便从控件接收事件。收到事件时,调用另一个控件上的方法来设置值。



请考虑以下示例:

要保留的类事件参数

One way to do this is that you publish an event on the user control and wire that event on the window level in order to receive events from the control. When an event is received, call a method on the other control to set the value.

Consider the following example:
Class to hold event arguments
public class SomeChangeEventArgs : System.EventArgs {
   public string TextValue { get; set; }
}



用户控制XAML


User control XAML

<UserControl x:Class="MyNamespace.UserControl1"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

             xmlns:local="clr-namespace:MyNamespace"

             mc:Ignorable="d" 

             d:DesignHeight="300" 

             d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBox Grid.Row="0" x:Name="MyTextBox"/>
        <Button Grid.Row="1" x:Name="TheButton" Content="Press me..." Click="TheButton_Click"/>
    </Grid>
</UserControl>



用户控制cs


User control cs

using System;
using System.Windows;
using System.Windows.Controls;

namespace MyNamespace {
   /// <summary>
   /// Interaction logic for UserControl1.xaml
   /// </summary>
   public partial class UserControl1 : UserControl {

      public event EventHandler<SomeChangeEventArgs> SomethingHasChanged;

      public UserControl1() {
         InitializeComponent();
      }

      private void TheButton_Click(object sender, RoutedEventArgs e) {
         this.SomethingHasChanged?.Invoke(
            this, new SomeChangeEventArgs() { 
               TextValue = this.MyTextBox.Text 
            }
         );
      }

      public void SetTextValue(string newText) {
         this.MyTextBox.Text = newText;
      }
   }
}



主窗口XAML


Main window XAML

<Window x:Class="MyNamespace.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:MyNamespace"

        mc:Ignorable="d"

        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <local:UserControl1 Grid.Column="0" x:Name="FirstControl" SomethingHasChanged="First_SomethingHasChanged"/>
        <local:UserControl1 Grid.Column="1" x:Name="SecondControl" SomethingHasChanged="Second_SomethingHasChanged"/>
    </Grid>
</Window>



和主窗口cs


And the main window cs

using System.Windows;

namespace MyNamespace {
   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window {
      public MainWindow() {
         InitializeComponent();

      }

      private void First_SomethingHasChanged(object sender, SomeChangeEventArgs e) {
         this.SecondControl.SetTextValue(e.TextValue);
      }

      private void Second_SomethingHasChanged(object sender, SomeChangeEventArgs e) {
         this.FirstControl.SetTextValue(e.TextValue);
      }
   }
}


这篇关于如何在主窗口中绑定两个usercontrol(WPF / C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆