Update TextBox text when a ComboBox's value is changed

David Alan Condit

My app: A Store has a Products collection and an int Id property. A Product has string ProductCode and Description properties. A ViewModel references a single instance of Store. ViewModel has a static collection of Products (ProductList). My View's DataContext is set to ViewModel. My View displays Store via the ViewModel.

What works: My View has a TextBlock bound to Store's Id.

What doesn't work: My View has a DataGrid for adding Products to Store's Products collection. The DataGrid for adding Products to Store's Products collection allows me to choose a new ProductCode, using a DataGridComboBoxColumn column. This works fine. However, I want my chosen ProductCode to update the DataGridTextBoxColumn which is bound to a Product's Description.

I've spent hours searching the net and I haven't found anything that quite matches my scenario, except maybe "Example 12" from this link, but I haven't gotten it to work for my project: Best ComboBox Tutorial Ever

Solved:

The most significant change to my code is a RefreshDescription method that fires after ProductCode is set for a Product. This method takes ProductCode as an argument and queries a static ProductList to find the first matching Description. The user can change the auto-populated Description property if desired.

Code snippet from my view:

<StackPanel>
    <TextBlock Text="{Binding Store.Id}"/>
    <DataGrid ItemsSource="{Binding Store.Products}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridComboBoxColumn 
                Header="ProductCode"
                ItemsSource="{x:Static m:ItemsProvider.ProductList}" 
                SelectedValueBinding="{Binding ProductCode,  UpdateSourceTrigger=PropertyChanged}" 
                SelectedValuePath="ProductCode"
                DisplayMemberPath="ProductCode"/>
        <!-- 
        I want Description for my chosen Product to pop in automatically... but how? 
        -->
            <DataGridTextColumn Header="Description" Binding="{Binding Description}"/>
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>

My ViewModel:

public class ViewModel : NotifyObject
{
    // Constructor
    public ViewModel()
    {
        _store = new Store() { Id = 1 };
    }
    
    // Fields
    Store _store;
    Product _selectedProduct;
    
    // Properties
    public Store Store {
        get { return _store; }
        set {
            _store = value;
            base.NotifyPropertyChanged("Commission");
        }
    }   
}

My Product model:

public class Product : NotifyObject
{
    // Constructor
    public Product() { }
    
    // Fields
    string _productCode;
    string _description;
    
    // Properties
    public string ProductCode { 
        get { return _productCode; }
        set {
            _productCode = value;       
            base.NotifyPropertyChanged("ProductCode");
            RefreshDescription(ProductCode);
        }
    }
    
    public string Description {
        get { return _description; }
        set {
            _description = value;
            base.NotifyPropertyChanged("Description");
        }
    }
    
   // Private Methods
    void RefreshDescription(string productCode)
    {
        if (ItemsProvider.ProductList.Count == 0) {
            return;
        }
        Product product = ItemsProvider.ProductList.FirstOrDefault(p => p.ProductCode == productCode);
        this.Description = (product == null ? "" : product.Description);
    }
}

My Store model:

public class Store : NotifyObject
{
    // Constructor
    public Store()
    {
        Products = new ObservableCollection<Product>();
    }
    
    // Fields
    int _id;
    
    // Properties       
    public int Id { 
        get { return _id; }
        set {
            _id = value;
            base.NotifyPropertyChanged("Id");
        }
    }
    
    public ObservableCollection<Product> Products { get; set; }     
}

My static class for getting a list of products to choose from:

public static class ItemsProvider
{
    static ObservableCollection<Product> _productList = new ObservableCollection<Product>();
    static ItemsProvider() 
    {
        _productList = new ObservableCollection<Product>() {
            new Product() { ProductCode = "111", Description = "a" },
            new Product() { ProductCode = "222", Description = "b" },
            new Product() { ProductCode = "333", Description = "c" },
            new Product() { ProductCode = "444", Description = "d" }                    
        };
    }
    public static ObservableCollection<Product> ProductList {
        get {
            return _productList;
        }           
    }           
}
David Alan Condit

From my edited question:

The most significant change to my code is a RefreshDescription method that fires after ProductCode is set for a Product. This method takes ProductCode as an argument and queries a static ProductList to find the first matching Description. The user can change the auto-populated Description property if desired.

Thank you again for those who helped me to come to this conclusion!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

if combobox text changed reset all the textbox in the form

From Dev

Update QAbstractTableModel when combobox changed

From Dev

How do I add the tag value and the text value of multiple TextBox, only when the text has changed in WPF?

From Dev

highlight combobox item if it's text starts with the text of the combobox's textbox

From Dev

Fire TextChanged for textbox when the text changed by javascript

From Dev

How to set a TextBox value when another TextBox value is changed

From Dev

how to get text to be changed in one textbox in asp.net when text changed in other textbox

From Dev

Textbox text change event not firing when text changed from code

From Dev

Textbox text change event not firing when text changed from code

From Dev

Entity framework update with textbox.text value

From Dev

On Text Changed Client side event in telerik textBox to access a value

From Dev

asp.net textbox changed event not fired when textbox value is changed in jquery code

From Java

Update scope value when service data is changed

From Dev

call method when text is changed without textbox loosing focus

From Dev

Update textbox value when a button is clicked

From Dev

Update textbox value when a button is clicked

From Dev

remove checkbox and text value when combobox change

From Dev

cell text is not changing when segment value changed

From Dev

Update vue.js data automatically when binded input field's value is changed by JavaScript

From Dev

Update vue.js data automatically when binded input field's value is changed by JavaScript

From Dev

Enable TextBox when combobox's specific item is selected in MVVM

From Dev

Get text/value from textbox after value/text changed server side

From Dev

Can't update the static bound property's value when text box's text changes

From Dev

Update textBox.Text default value on a button click in WinForms

From Dev

Update textBox.Text default value on a button click in WinForms

From Dev

Text in a TextBox does not update instantly when change it in the code-behind

From Dev

MySQL no affected rows upon UPDATE when value not changed

From Dev

WPF - Value inside datagrid fails to update converter when changed externally

From Dev

How to update AngularJS view when the value has not changed?

Related Related

  1. 1

    if combobox text changed reset all the textbox in the form

  2. 2

    Update QAbstractTableModel when combobox changed

  3. 3

    How do I add the tag value and the text value of multiple TextBox, only when the text has changed in WPF?

  4. 4

    highlight combobox item if it's text starts with the text of the combobox's textbox

  5. 5

    Fire TextChanged for textbox when the text changed by javascript

  6. 6

    How to set a TextBox value when another TextBox value is changed

  7. 7

    how to get text to be changed in one textbox in asp.net when text changed in other textbox

  8. 8

    Textbox text change event not firing when text changed from code

  9. 9

    Textbox text change event not firing when text changed from code

  10. 10

    Entity framework update with textbox.text value

  11. 11

    On Text Changed Client side event in telerik textBox to access a value

  12. 12

    asp.net textbox changed event not fired when textbox value is changed in jquery code

  13. 13

    Update scope value when service data is changed

  14. 14

    call method when text is changed without textbox loosing focus

  15. 15

    Update textbox value when a button is clicked

  16. 16

    Update textbox value when a button is clicked

  17. 17

    remove checkbox and text value when combobox change

  18. 18

    cell text is not changing when segment value changed

  19. 19

    Update vue.js data automatically when binded input field's value is changed by JavaScript

  20. 20

    Update vue.js data automatically when binded input field's value is changed by JavaScript

  21. 21

    Enable TextBox when combobox's specific item is selected in MVVM

  22. 22

    Get text/value from textbox after value/text changed server side

  23. 23

    Can't update the static bound property's value when text box's text changes

  24. 24

    Update textBox.Text default value on a button click in WinForms

  25. 25

    Update textBox.Text default value on a button click in WinForms

  26. 26

    Text in a TextBox does not update instantly when change it in the code-behind

  27. 27

    MySQL no affected rows upon UPDATE when value not changed

  28. 28

    WPF - Value inside datagrid fails to update converter when changed externally

  29. 29

    How to update AngularJS view when the value has not changed?

HotTag

Archive