I want to change textbox1 which is in the usercontrol

Answered I want to change textbox1 which is in the usercontrol

  • 8. srpna 2012 21:48
     
     

    I have a button in Mainwindow.xaml. Its name is ( button1).

    In the Mainwindow.xaml there is a usercontrol which is loading dynamically.

    I want to change textbox1  which is  in the usercontrol;  when i click (button1).

    İ tried the way below but it does not work.

            private void button1_Click(object sender, RoutedEventArgs e)

            {

                UserControl altmenu = new alt_menu();

                ((TextBox)altmenu.FindName("textbox1")).Text = "Hi";

            }

     

    I will be happy if you help.

Všechny reakce

  • 8. srpna 2012 22:10
    Moderátor
     
     

    I would recommend exposing a property (as a string) in your UserControl, and setting the textbox Text within the property's setter.  This will help make it easier later if you want to change the internal implementation.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • 8. srpna 2012 22:12
    Moderátor
     
      Obsahuje kód

    I think, if I understand you, you have a UserControl called "alt_menu"
    That has a control which you gave an x:Name to.
    Then you can access it directly.
    The mistake I think you made was casting it as a UserContrtol, so it lost all identity.

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                var altmenu = new alt_menu();
                altmenu.textbox1.Text = "Hi";
            }
     
    Regards,
    Pete

    #PEJL



  • 8. srpna 2012 22:34
     
      Obsahuje kód

    I would recommend exposing a property (as a string) in your UserControl, and setting the textbox Text within the property's setter.  This will help make it easier later if you want to change the internal implementation.



    thank you for your help, but i can't understand because of my poor English. can you explain this with an example?

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                var altmenu = new alt_menu();
                altmenu.textbox1.Text = "Hi";
            }
     

    thank you for your help but this code did not work.

    I added usercontrol to Mainwindow.xaml as you see below.

    LayoutRoot.Children.Insert(0, altmenu);

  • 8. srpna 2012 23:02
    Moderátor
     
     Odpovědět Obsahuje kód

    If you added altmenu that way - you have to access that one, not create a new one:

     private void button1_Click(object sender, RoutedEventArgs e)
    {
        alt_menu altmenu = LayoutRoot.Children[0] as alt_menu;
        ((TextBox)altmenu.FindName("textbox1")).Text = "Hi";
    }



    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Označen jako odpověď frezmb 8. srpna 2012 23:23
    •  
  • 8. srpna 2012 23:07
    Moderátor
     
      Obsahuje kód

    My original suggestion was trying to provide a solution that matched your methods...

    <Window x:Class="WpfApplication52.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" Background="#FFF1EEEE">
        <Grid x:Name="LayoutRoot"/>
    </Window>


    using System.Windows;
    
    namespace WpfApplication52
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                var test = new UserControl1();
                test.test1.Text = "asd";
                LayoutRoot.Children.Add(test);
            }
        }
    }


    <UserControl x:Class="WpfApplication52.UserControl1"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Grid>
            <TextBox x:Name="test1"/>
        </Grid>
    </UserControl>

     

     

    Here is Reed's way of doing it, which I agree is the better:

    using System.Windows;
    
    namespace WpfApplication52
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                var test = new UserControl1 { UserName = "Reed" };
                LayoutRoot.Children.Add(test);
            }
        }
    }


    using System.Windows;
    using System.Windows.Controls;
    
    namespace WpfApplication52
    {
        public partial class UserControl1 : UserControl
        {
            public string UserName
            {
                get { return (string)GetValue(UserNameProperty); }
                set { SetValue(UserNameProperty, value); }
            }
    
            public static readonly DependencyProperty UserNameProperty =
                DependencyProperty.Register("UserName", typeof(string), typeof(UserControl1), new UIPropertyMetadata(null));
    
            public UserControl1()
            {
                InitializeComponent();
                DataContext = this;
            }
        }
    }


    <UserControl x:Class="WpfApplication52.UserControl1"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >
        <Grid>
            <TextBox Text="{Binding UserName}"/>
        </Grid>
    </UserControl>

    Hope that helps you understand the two methods.

    Regards,
    Pete


    #PEJL




  • 8. srpna 2012 23:23
     
      Obsahuje kód

    If you added altmenu that way - you have to access that one, not create a new one:

     private void button1_Click(object sender, RoutedEventArgs e)
    {
        alt_menu altmenu = LayoutRoot.Children[1] as alt_menu;
        ((TextBox)altmenu.FindName("textbox1")).Text = "Hi";
    }

     thanks, thanks, thanks
  • 8. srpna 2012 23:24
     
      Obsahuje kód

    My original suggestion was trying to provide a solution that matched your methods...

    <Window x:Class="WpfApplication52.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" Background="#FFF1EEEE">
        <Grid x:Name="LayoutRoot"/>
    </Window>


    using System.Windows;
    
    namespace WpfApplication52
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                var test = new UserControl1();
                test.test1.Text = "asd";
                LayoutRoot.Children.Add(test);
            }
        }
    }


    <UserControl x:Class="WpfApplication52.UserControl1"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Grid>
            <TextBox x:Name="test1"/>
        </Grid>
    </UserControl>

     

     

    Here is Reed's way of doing it, which I agree is the better:

    using System.Windows;
    
    namespace WpfApplication52
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                var test = new UserControl1 { UserName = "Reed" };
                LayoutRoot.Children.Add(test);
            }
        }
    }


    using System.Windows;
    using System.Windows.Controls;
    
    namespace WpfApplication52
    {
        public partial class UserControl1 : UserControl
        {
            public string UserName
            {
                get { return (string)GetValue(UserNameProperty); }
                set { SetValue(UserNameProperty, value); }
            }
    
            public static readonly DependencyProperty UserNameProperty =
                DependencyProperty.Register("UserName", typeof(string), typeof(UserControl1), new UIPropertyMetadata(null));
    
            public UserControl1()
            {
                InitializeComponent();
                DataContext = this;
            }
        }
    }


    <UserControl x:Class="WpfApplication52.UserControl1"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >
        <Grid>
            <TextBox Text="{Binding UserName}"/>
        </Grid>
    </UserControl>

    Hope that helps you understand the two methods.

    Regards,
    Pete


    #PEJL




    XAML guy Thank you for everything

  • 12. srpna 2012 15:46
     
     Navržená odpověď

    First you should load the usercontrol and then try: for ex:

    UserControl altmenu = new alt_menu();

    stackPanel.Children.Add(altmenu);

    use VisualTreeHelper class to find children, it will solve your problem

    __________________________________________________________

    Do vote the Article and propose the answer

    • Navržen jako odpověď kishhr 12. srpna 2012 15:46
    •