locked
Binding to Element within Xaml RRS feed

  • Question

  • I have two textboxes in my xaml e.g. TB1 and TB2. I want the Tag property of TB1 to store TB2 object. How this can be achieved through Xaml? I tried using ElementName and Source both in the binding but it is not working.

    Thanks,


    Krunal C

    Monday, September 3, 2012 9:59 AM

Answers

  • What exactly did you try? The following appears to work fine for me (assuming I understood correctly what you are trying to do):

    <TextBox x:Name="tb1" />
    <TextBox Tag="{Binding ElementName=tb1}" />

    • Proposed as answer by agrawal.ashish Monday, September 3, 2012 10:12 AM
    • Marked as answer by KrunalC Tuesday, September 4, 2012 8:35 AM
    Monday, September 3, 2012 10:07 AM

All replies

  • What exactly did you try? The following appears to work fine for me (assuming I understood correctly what you are trying to do):

    <TextBox x:Name="tb1" />
    <TextBox Tag="{Binding ElementName=tb1}" />

    • Proposed as answer by agrawal.ashish Monday, September 3, 2012 10:12 AM
    • Marked as answer by KrunalC Tuesday, September 4, 2012 8:35 AM
    Monday, September 3, 2012 10:07 AM
  • Hi KrunalC,

    Use below code

    <TextBox x:Name="TB1" Tag="{Binding ElementName=TB2}" />

    <TextBox x:Name="TB2"   />

    Here is the sample code

    XAML

    <Window x:Class="WpfApplication29.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>
            <Button Content="check" VerticalAlignment="Top" Click="Button_Click"/>
            <TextBox x:Name="TB1" Tag="{Binding ElementName=TB2}" VerticalAlignment="Center" />
            <TextBox x:Name="TB2" VerticalAlignment="Bottom"/>
        </Grid>
    </Window>

    .cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

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

            private void Button_Click(object sender, RoutedEventArgs e)
            {            
                TextBox tBox = (TB1.Tag as TextBox);
                tBox.Text = "Change in code";
            }
        }
    }

    Regards

    Agrawal

    -------------------------------------------------------------------------

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".




    • Edited by agrawal.ashish Monday, September 3, 2012 10:21 AM Added Sample code
    Monday, September 3, 2012 10:08 AM
  • Dear Mike,

    The code you suggested above does work. However, my scenario was bit different and in order to simplify the question I asked with the example of two text boxes. Thanks for your reply.


    Krunal C

    Tuesday, September 4, 2012 8:35 AM