5 Replies - 9184 Views - Last Post: 23 January 2013 - 02:39 PM Rate Topic: -----

#1 ysrinu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-June 12

Bind elements to User Control objects

Posted 17 June 2012 - 10:26 AM

I am beginner to WPF and searched existing forums for binding with UserControl and still need help with this:
If you provide a simple working example that would be great:

I have a main window with textboxes for system logon, the values of which should be passed over to user control UserControlEnv.
The Main Window can call multiple 'instances' of UserControlEnv (say DEV, UAT, PRD)
Now UserControlEnv itself has text boxes for Database login and it should pass values of these and the ones it got from Main Window to another user control UserControlDb

I have attached pdf file for the one-way mode binding relation.
Whenever the source is updated, the target should be updated and a change event should be fired in the child user control so we can take custom action (like recreating the connection string)

Thanks,
-srinivas yelamanchili

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Bind elements to User Control objects

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,359
  • Joined: 02-June 10

Re: Bind elements to User Control objects

Posted 17 June 2012 - 10:38 AM

Have you tried coding anything yourself, or did you just come here hoping we would do it for you?

In other words, as it says, we won't give you code but will help with your code. Show us what you have done so far.
Was This Post Helpful? 0
  • +
  • -

#3 ysrinu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-June 12

Re: Bind elements to User Control objects

Posted 17 June 2012 - 12:00 PM

apologize for not attaching my code earlier.
Please find the attached, for simplicity i only included the Main Window and UserControlEnv code.

a. strEnvName of UserControlEnv is set only once when calling this from Main window.
The UserControlEnv has to display this in it's TextBlock
This isn't working

b. The passwordBox doesn't have Text property. What's the right way to bind password values securely?

<UserControl x:Class="WpfApplicationLogOn.UserControlEnv"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBlock Text="{Binding strEnvName}" ></TextBlock>
        <!--<TextBox Name="tbDbUserName" Margin="0,0,355,0"></TextBox>
        <PasswordBox Name="pbDbPassword" Grid.Row="1" Margin="0,0,355,0"></PasswordBox>-->
    </Grid>
</UserControl>


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;
using System.Security;

namespace WpfApplicationLogOn
{
    /// <summary>
    /// Interaction logic for UserControlEnv.xaml
    /// </summary>
    public partial class UserControlEnv : UserControl
    {
        private string _strEnvName;
        public string strEnvName
        {
            get { return _strEnvName; }
            set { _strEnvName = value; }
        }
        public static readonly DependencyProperty strEnvNameProperty = DependencyProperty.Register(
            "strEnvName", typeof(string), typeof(UserControlEnv), null);

        private string _strLdapUserName;
        public string strLdapUserName
        {
            get {return _strLdapUserName;}
            set {_strLdapUserName = value;}
        }
        public static readonly DependencyProperty strLdapUserNameProperty = DependencyProperty.Register(
            "strLdapUserName", typeof(string), typeof(UserControlEnv), new FrameworkPropertyMetadata(new PropertyChangedCallback(logonchanged)));

        private SecureString _sstrLdapPassword;
        public SecureString sstrLdapPassword
        {
            get { return _sstrLdapPassword; }
            set { _sstrLdapPassword = value; }
        }
        public static readonly DependencyProperty sstrLdapPasswordProperty = DependencyProperty.Register(
            "sstrLdapPassword", typeof(string), typeof(UserControlEnv), new FrameworkPropertyMetadata(new PropertyChangedCallback(logonchanged)));


        private static void logonchanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //do stuff here
            //MessageBox.Show("Changed Property " + e.Property.ToString());
        }

        public UserControlEnv()
        {
            InitializeComponent();
        }
    }
}



<Window x:Class="WpfApplicationLogOn.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplicationLogOn"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBox Name="tbLdapUserName" Margin="0,0,355,0"></TextBox>
        <PasswordBox Name="pbLdapPassword" Grid.Row="1" Margin="0,0,355,0"></PasswordBox>
        <TabControl Grid.Row="2">
            <TabItem>
                <local:UserControlEnv strEnvName="DEV"
                    strLdapUserName="{Binding ElementName=tbLdapUserName, Path=Text, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" 
                    sstrLdapPassword="{Binding ElementName=pbLdapPassword, Path=Text, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" >
                </local:UserControlEnv>
            </TabItem>
            <TabItem>
                <local:UserControlEnv strEnvName="UAT"
                    strLdapUserName="{Binding ElementName=tbLdapUserName, Path=Text, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" 
                    sstrLdapPassword="{Binding ElementName=pbLdapPassword, Path=Text, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" >
                </local:UserControlEnv>
            </TabItem>
        </TabControl>
    </Grid>
</Window>


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 WpfApplicationLogOn
{
    /// <summary>
    /// Interaction logic for Mainwindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}



Thanks,
-sri
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,359
  • Joined: 02-June 10

Re: Bind elements to User Control objects

Posted 17 June 2012 - 12:15 PM

Quote

The passwordBox doesn't have Text property.

Right. It has a .Password property instead. And raises a .PasswordChanged event.

Have you read the MSDN documenation (with code examples) for the PasswordBox? The MSDN should always be your first stop.

Another good tutorial can be found here:
http://wpftutorial.n...asswordBox.html
Was This Post Helpful? 0
  • +
  • -

#5 ysrinu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-June 12

Re: Bind elements to User Control objects

Posted 17 June 2012 - 02:56 PM

thank you.
Regarding (a), why isn't TextBlock in UserControlEnv has text from it's local object strEnvName ?
This strEnvName is set when MainWindow called the UserControl

MainWindow (xaml) : line 16 and 22
UserControlEnv (xaml): line 14

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,359
  • Joined: 02-June 10

Re: Bind elements to User Control objects

Posted 23 January 2013 - 02:39 PM

Yeah - so? Just make a property on the UserControl and bind to that. Its not any different than binding to the text property of the TextBox. Its just a property of an object.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1