Thursday 15 January 2015

c# - How do I bind controls to the selected item in a listbox using WPF and the MVVM pattern? -


I currently have a working method, but I do not work for what I need. The Settings window appears here:

Right now I To compute an ObservableCollection of the department objects, the Database tab is bound to the DataContext property, and then I just see the code in the text box to correct the properties of the section: here the code is provided for this tab:

  & Tab; Tititum header = "department settings" datacontax = "{binding section}" & gt; & Lt; DocPanel margin = "3,3,3,3" & gt; & Lt; DockPaneel DockPanel.Doc = "Left" width = "200" & gt; & Lt; StackPanel DockPanel.Dock = "bottom" margin = "0,5,0,0" orientation = "horizontal" & gt; & Lt; Text box x: name = "tbxAddDepartmentName" width = "160" padding = "0,5,0,5" /> & lt; Button x: name = "btnAddDepartment" content = "add" margin = "5,0,5,0" padding = "5,5,5,5" command = "{Binding AddDepartmentCommand, UpdateSourceTrigger = PropertyChanged}" /> ; & Lt; / StackPanel & gt; & Lt; List Box Itemsource = "{Binding}" /> & Lt; / DockPanel & gt; & Lt; StackPanel DockPanel.Dock = "Down" & gt; & Lt; Button content = "save changes" padding = "5,5,5,5" horizontal alignment = "right" /> & Lt; / StackPanel & gt; & Lt; Grid margin = "5" & gt; & Lt; Grid.RowDefinitions & gt; & Lt; RowDefinition Height = "Auto" /> & Lt; RowDefinition Height = "Auto" /> & Lt; RowDefinition Height = "Auto" /> & Lt; /Grid.RowDefinitions> & Lt; Grid.ColumnDefinitions & gt; & Lt; Column width = "150" /> & Lt; Column width = "*" /> & Lt; /Grid.ColumnDefinitions> & Lt ;! - Labels - & gt; & Lt; Label grid Rau = "0" grid. Column = "0" & ​​gt; Department name: & lt; / Labels & gt; & Lt; Label grid Rows = "1" grid. Column = "0" & ​​gt; Report Number: & lt; / Labels & gt; & Lt; Label grid Line = "2" grid Column = "0" & ​​gt; Address: & lt; / Labels & gt; & Lt ;! - Input - & gt; & Lt; Text Box X: Name = "tbxDepartmentName" text = "{binding name}" grid. Line = "0" grid Column = "1" vertical content element = "center" margin = "0,5,0,5" padding = "5" /> & Lt; Text box grid Rows = "1" grid. Column = "1" text = "{binding report number}" Vertical content element = "center" margin = "0,5,0,5" padding = "5" /> & Lt; Text box grid Rows = "2" grid. Column = "1" text = "{binding address}" vertical content element = "center" accepts return = "true" margin = "0,5,0,5" padding = "5" "/" & gt; & lt ; / Grid>    

What do I want to do, is an indicator for a specific department object that I The view is selected in the model with the list box. In this way, I should be able to correctly bind the text box, and "change co After pressing the button, avoid the change in SQL compact DB.

see sample modal Public class ViewModel: INotifyPropertyChanged, INotifyPropertyChanging {Public class MyObj {Public string} set;} Public string Test2 {get; set;}} # Region Selected ITM Private MyObj Selected I Public MyObj SelectedItem {get {return_selectedItem;} set {if (_selectItem! = Value) {NotifyPropertyChanging ("selectedItem"); _selectedItem = value; NotifyPropertyChanged ("SelectedItem"); }}} #endrian #region INotifyPropertyChanged Member Public Event PropertyChangedEventHandler PropertyChanged; // If the data reference property that has been used to inform protected zeros has been changed, NotifyPropertyChanged (string propertyName) {if (PropertyChanged! = Null) {PropertyChanged (New PropertyChangedEventArgs (propertyName)); }} # Interval # regional notification amputating changeing member public event property-sharing diance handler property charging; // Data reference is used to inform that data reference properties are about to change the Zero NotifyPropertyChanging (string propertyName) {if (PropertyChanging! = Null) {Property Charges (this, New PropertyChangingEventArgs (propertyName)); }} #endregion}

Add to your listbox:

  selected item = "{binding selected item, UpdateSourceTrigger = PropertyChanged}"   

Your data context must be the whole view modal, if you do not have the archive, you can set the list in the box

  items source = "{binding section}"    DataContext = "{binding section}">    

No comments:

Post a Comment