Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 5-May-18 8:20am

1 solution

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
C#
public class SomeChangeEventArgs : System.EventArgs {
   public string TextValue { get; set; }
}

User control XAML
XML
<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>

User control cs
C#
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;
      }
   }
}

Main window XAML
XML
<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>

And the main window cs
C#
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);
      }
   }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900