Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4110720
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:58:14+00:00 2026-05-20T21:58:14+00:00

I need to bind a TextBox’s Text to a property of a class that

  • 0

I need to bind a TextBox’s Text to a property of a class that itself a property of my DataContext. So, for example if I have a class called ServerInstance and it has a string property called Name. I expose a ServerInstance as a property called SelectedInstance in my code behind (viewmodel in this case). My question is how can I bind a TextBox to the Name property of the SelectedInstance property?

Here is my ViewModel/Code behind:

public class ViewModel : Notifier
{
    private MobileServerInstance m_instance = null;
    private RelayCommand m_addCommand = null;
    private RelayCommand m_clearCommand = null;

    public MobileServerInstance ServerInstance
    {
        get { return m_instance; }
        set { m_instance = value; OnPropertyChanged("ServerInstance"); }
    }

    public ICommand AddCommand
    {
        get
        {
            if (m_addCommand == null)
            {
                m_addCommand = new RelayCommand(parameter=>Add(parameter), parameter=>CanAdd(parameter));
            }

            return m_addCommand;
        }
    }

    public ICommand ClearCommand
    {
        get
        {
            if (m_clearCommand == null)
            {
                m_clearCommand = new RelayCommand(parameter => Clear(parameter), parameter => CanClear(parameter));
            }

            return m_clearCommand;
        }
    }

    private bool CanClear(object parameter)
    {
        return m_instance != null;
    }

    private void Clear(object parameter)
    {
        m_instance = null;
    }

    private bool CanAdd(object parameter)
    {
        return m_instance == null;
    }

    private void Add(object parameter)
    {
        m_instance = new MobileServerInstance();
    }
}

Notifier is a base class that implements the INotifyPropertyChanged interface and provides a protected OnPropertyChanged method which raises the PropertyChange event – typical pattern there.

Here is my simple class that I use for the DataContext:

public class MobileServerInstance : Notifier
{
    private string m_name = "Name";
    private string m_alias = "Alias";

    public string Name
    {
        get { return m_name; }
        set { m_name = value; OnPropertyChanged("Name"); }
    }

    public string Alias
    {
        get { return m_alias; }
        set { m_alias = value; OnPropertyChanged("Alias"); }
    }
}

And finally my xaml (this is a POC so the UI is very simple):

<Window x:Class="WpfApplication4.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:WpfApplication4"
    Title="Window1" Height="300" Width="500">

    <Window.DataContext>
        <src:ViewModel />
    </Window.DataContext>

    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label Content="Name:" Margin="0 0 2 0" />
            <ComboBox Margin="0 0 8 0" Width="125" />
            <Button Margin="0 0 4 0" Content="Add" Command="{Binding AddCommand}" Width="75" />
            <Button Content="Clear" Command="{Binding ClearCommand}" Width="75" />
        </StackPanel>

        <StackPanel Margin="20 5 20 5"">
            <StackPanel Orientation="Horizontal">
                <Label Width="40" Content="Name:" Margin="0 0 2 0" />
                <TextBox Width="250" Text="{Binding ServerInstance.Name}" />
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="0 5 0 0">
                <Label Width="40" Content="Alias:" Margin="0 0 2 0" />
                <TextBox Width="250" Text="{Binding ServerInstance.Alias}" />
            </StackPanel>
        </StackPanel>
    </StackPanel>
</Window>

Thanks, in advance, for any help you can provide.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:58:15+00:00Added an answer on May 20, 2026 at 9:58 pm

    {Binding Path=ServerInstance.Name} must work, of course to see Name changing value even ServerInstance should be an INotifyPropertyChanged.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to bind the text property of a textbox that it is located
I need to bind a TextBox that meets two criteria: IsEnabled if Text.Length >
I need to bind width property of the GridView column that is created dynamically
I have this GridView that I'm trying to bind data to and I need
I have a textbox <input name=tx1 size=10 type=text id=tx1 class=sample dynamic format maxlength=10 />
I need to bind a property of a UserControl directly to another control, rather
I need to bind to a Storyboard, but I cant figure how. I have
I have a situation where I need to bind a click event to an
I have a textbox that I am defining as <%= Html.TextBox(Username, Model.Form.Username, new {
I need help in changing opacity of text in textbox.As of now I'm able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.