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 8724031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:44:48+00:00 2026-06-13T07:44:48+00:00

I am working on a textbox and combobox in my wpf app. I am

  • 0

I am working on a textbox and combobox in my wpf app. I am sorry for weird title. Well here is the scenario:

Xaml:

<ComboBox ItemsSource="{Binding PortModeList}" SelectedItem="{Binding SelectedPortModeList, Mode=OneWayToSource}" SelectedIndex="0" Name="PortModeCombo" />

<TextBox Grid.Column="1" Text="{Binding OversampleRateBox}" Name="HBFilterOversampleBox" />

ViewModel Class:

public ObservableCollection<string> PortModeList
    {
        get { return _PortModeList; }
        set
        {
            _PortModeList = value;
            OnPropertyChanged("PortModeList");
        }
    }

    private string _selectedPortModeList;
    public string SelectedPortModeList
    {
        get { return _selectedPortModeList; }
        set
        {
            _selectedPortModeList = value;                
            OnPropertyChanged("SelectedPortModeList");
        }
    }

    private string _OversampleRateBox;
    public string OversampleRateBox
    {
        get
        {
            return _OversampleRateBox;
        }

        set
        {
            _OversampleRateBox = value;
            OnPropertyChanged("OversampleRateBox");
        }
    }

Here I have three requirements:

  1. By using SelectedIdin xaml I am able to select the id but I want to set the selectedid of my combobox from viewmodel class. I.e.
    int portorder = 2
    PortModeList->SetSelectedId(portOrder)
    . How can I do something like this? or is their any other approach?

  2. I need to restrict number of entries inside a textbox to 4. I.e. 1234 is entered in textbox, it should not let user exceed 4 digits.

  3. I want to set the format of text in OversampleRateBox as: 0x__. I.e. if user wants to enter 23 present in a variable, then I shud set text as 0x23. Basically 0x should be present at the beginning.

Please help 🙂

  • 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-06-13T07:44:49+00:00Added an answer on June 13, 2026 at 7:44 am

    I would use the SelectedItem property of the ComboBox (as you are doing), but make the binding two-way. Then, you can set your SelectedPortModeList (which should be called SelectedPortMode) in your view model.

    <ComboBox ItemsSource="{Binding PortModeList}" 
           SelectedItem="{Binding SelectedPortMode}" ...
    

    In the view model:

    // Select first port mode
    this.SelectedPortMode = this.PortModeList[0];
    

    If you want to limit the number of characters in a TextBox, then use the MaxLength property:

    <TextBox MaxLength="4" ... />
    

    If you wish to add a prefix to the OversampleRateBox, one option would be to test for the presence of this in the setter for OversampleRateBox, and if it’s not there, then add it before assigning to your private field.

    Update

    Bind your TextBox to a string property:

    <TextBox Grid.Column="1" Text="{Binding OversampleRateBox}" ... />
    

    In the setter for your property, check that the input is valid before setting your private field:

    public string OversampleRateBox
    {
        get
        {
            return _OversampleRateBox;
        }
    
        set
        {
            // You could use more sophisticated validation here, for example a regular expression
            if (value.StartsWith("0x"))
            {
                _OversampleRateBox = value;
            }
    
            // Now if the value entered is not valid, then the text box will be refreshed with the old (valid) value
            OnPropertyChanged("OversampleRateBox");
        }
    }
    

    Because the binding is two-way, you can also set the value from your view model code (for example in the view model constructor):

    this.OversampleRateBox = "0x1F";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following textbox where the binding is working just fine until I
I'm working on a WPF MVVM application and I've got a TextBox on my
I am a C++ developer and recently started working on WPF. I am sorry
I am working on WPF application. I have a window which has Combobox and
I am working with editable combobox where text can be entered in the textbox
I am currently working on a WPF TimePicker Control. The control inherits a TextBox
I have a user control with a ComboBox and a TextBox. Everything is working
I have this custom textbox that I am working on and I can use
I have a ASP.NET textbox autocomplete with JQuery, is working perfectly, i type part
I'm working on a project where, i must provide the user a textbox where

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.