The origin of a Biding WPF can be updated

0

to see if they could help me. I have two TextBox each in a different class, and I need that what is written in one is shown in the other.

The one that should receive the data, I have it in xaml:

<TextBox x:Name="mTxtPuntosLocal" Text="{Binding AddPuntoLocal, ElementName=ventana}" HorizontalAlignment="Left"  TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="100"  Background="{x:Null}" BorderBrush="{x:Null}"  Width="135"/>

Right now I take the data to prove that it receives like this:

string varPuntoLocal;       
        public string AddPuntoLocal
        {
            get
            {
                return  "3";
            }
            set
            {
                varPuntoLocal = value;
            }
        }

Right now I receive the "3" always. I need you to show me what is inside another textbox of another class, as you should reference it in Biding.

Thanks

    
asked by Fran Pino 18.08.2016 в 12:37
source

1 answer

1

I found the answer to your question in StackOverflow in English. I hope you serve here I leave the link and I'll copy the information .

  

What you have to do is pass a reference to the first window or   the object that will update the text property to the second   window, which is property DataContext will do so, then   You can force the second controls of the windows to the same.

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public string TestString
    {
        get { return (string)GetValue(TestStringProperty); }
        set { SetValue(TestStringProperty, value); }
    }

    public static readonly DependencyProperty TestStringProperty =  DependencyProperty.Register("TestString", typeof(string), typeof(MainWindow), new UIPropertyMetadata(null));

    public MainWindow()
    {
        InitializeComponent();

        // setup the test string.
        TestString = "this is a test.";

        // Create the second window and pass this window as it's data context.
        Window1 newWindow = new Window1()
        {
            DataContext = this
        };
        newWindow.Show();
    }
}

MainWindow.xaml - Take note of the DataContext line in the window declaration.

<Window x:Class="WpfApplication5.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"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        >
    <Grid>
        <TextBox Text="{Binding TestString, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="91,84,185,189" />
    </Grid>
</Window>

Window1.xaml

<Window x:Class="WpfApplication5.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <TextBlock Text="{Binding TestString, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>

    
answered by 07.11.2016 / 17:40
source