My C# winforms project has 2 controls that I made.
PersonFormControl and PersonListControl
I have placed these controls on a form called PersonForm
There is also a bindingSource on the form.
How do I bind both PersonFormControl and PersonListControl to the same bindingSource?
I have been able to work around my issue by making the bindingsource.Datasource properties all point to the same object
Related
I have a form in my project that has the controls created through use of a class. There's an issue with this way of it though, since any of the controls I will create will be under an ItemsControl control, I'm not quite sure how I would access the events of any of the controls I create(textbox for example)
If I want to create a KeyPress event for my textbox, I have an error in the code:
Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
I'm only assuming this is because of my ItemsControl because I've ran into this same issue when trying to access controls' properties from within the ItemsControl and it was a nightmare to fix. What's the real reason this is throwing an error?
Your problem is that you're trying to put this functionality in the wrong place.
The code behind the Window that contains your ItemsControl is not the right place to put code which affects / is concerned about a particular UI element which is reused over and over in an ItemsControl. You need to isolate the "pieces" of your UI and create modular, individual units of functionality / Visuals that can be reused and work independently of their "surrounding" environment.
In this case, you have to replace the TextBox in your DataTemplate (in XAML) for another UI element which performs like a TextBox, but only allows numeric / masked input.
There are 3 different approaches to that:
1 - replace the TextBox for a MaskedTextBox from the WPF toolkit.
2 - replace the TextBox for a UserControl that contains the TextBox and uses code behind to perform the numeric-only input functionality.
3- replace the TextBox for a Custom Control that inherits from TextBox and performs the input validation in on it's own.
The difference between approaches 2 and 3 is that a UserControl is a composite piece of UI made up of XAML + Code Behind, which can contain any other UI elements, whereas a Custom Control is a code-only (no XAML / "Lookless") class that is derived from a Control (such as TextBox) which requires a ControlTemplate to function properly.
Notice that neither of these approaches involve putting code behind the Window that contains your itemsControl. It is simply a matter of separation of concerns and proper encapsulation of functionality.
You need to subscribe to the event dynamically, using the AddHandler keyword:
Dim tb As New TextBox
...
AddHandler tb.TextChanged, AddressOf tb_TextChanged
where tb_TextChanged is the method that handles the event:
Private Sub tb_TextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
...
End Sub
If you need to unsubscribe, use the RemoveHandler keyword.
I searched this site and i found 2 Links
how to load wpf usercontrol in MVVM pattern
MVVM-Light: Load UserControl into Window
but still i cant find the answer to my problem and this link
MVVM-Light Locator Pattern and Reusable UserControl
i Didn't Understood.... so here is i am stating my problem which might help others struggling like me ......
I have a MainWindow which has 2 parts one has a TreeView(a
UserControl) and the other Displays different user controls(named DisplayPanel).... just like windows Explorer.
The Display Panel on the Right side will display different user controls on Clicking nodes of tree view.
and my TreeView is Itself a user Control.
How can i make this composite UI Work using MVVM. Also I am planning to use MVVM light Toolkit. Does this have something that can help...
An Example will be great
Thanks... :)
Edit
My TreeView in a UserControl I made a dependency property in the UserControl which catches the selected Item fo the tree view so that i can use this dependency property to populate the required view in the "MainView" ContentControl binding....as you advised me in the comments. Everything is till now
Problem is that i want to display data contained in the the selected item and i cannot set the DataContext of the UserControls(which will be displayed in right hand side) to the selected item as then i will not be able to use my view model for the respective usercontrol for commands and other operations
I tried to solve this too.... i used the Mediator (Messenger) in my TreeViewUserControl view model to send a Message to the Usercontrol's(the one that i need to display) view model . Message will be passed whenever the item is selected in the tree view. and message contains the selected node. I forgot to mention i set the datacontext of the UserControl to its view model so that i could display data
But using this approach the problem is that when the I click a type of node for the first time the data is not populated but if the same type of node is clicked again its populated. What’s happening is that UserControls object is availabe when the the tree item is clicked for the first time and Mediator sends the message. So Mediator is not able to pass the message to the userControl view model.....
I totally do not have ne idea to solve this further.... is my way if displaying user control right or I should do something that else....totally confused.....
You could try defining a DataTemplate for each type in the TreeView's ItemsSource and instead of having a specific UserControl on the right side, just bind to the TreeView's SelectedItem. Alternatively, you could use a DataTemplateSelector.
Edited for OP's Edit
Did you do this?
MainWindow has TreeView whose ItemsSource=Binding MainVM.Items.
MainWindow has ContentControl whose Content=Binding TreeView.SelectedItem.
Somewhere in project, have ResourceDictionary where each possible type in MainVM.Items has a DataTemplate defined?
Which ViewModel (MainVM or ItemVM) are you trying to use and why can't you use it?
I have a Silverlight User Control. This contains a DataForm. This DataForm nests another Silverlight User Control, which also contains a DataForm.
I have bound the nested dataform successfully to an object in the parent control.
I can NOT however, get the nested control to enter Edit mode along with the parent. I have tried calling it directly by using the FindNameInContent method to return the nested dataform and then called BeginEdit() but this fails (returns false).
Stuck.
I had a simpler scenario but a similar problem. In my scenario I had a DataGrid directly nested inside a DataForm, and was unable to synchronize the edit modes of the two controls until I realised that I needed to implement an EditTemplate for the DataForm, as well as the ReadOnlyTemplate, and implement the DataGrid in both. I set IsReadOnly=true for the DataGrid in the ReadOnlyTemplate and IsReadOnly=false for the DataGrid in the EditTemplate, and it all works smoothly, with no code.
Dave.
This is using winforms.
I have a listbox and a combo box, both tied to the same datasource, same display members, same value members. All is bound just fine and the items show up in both the controls.
The problem is when I change a selection in one control, it moves to the same index in the other control. I don't have any events tied to either control. It is just happening on its own. Has anyone ever run into this?
This is because both the controls share the same BindingContext/CurrencyManager. Controls inherit the BindingContext from their container control. A BindingContext maintains only one CurrencyManager per DataSource. If you want to have two different CurrencyManagers, you need to have two BindingContexts.
So when once of the controls selection is changed, currencyManagaer.Current gets updated. This affects all the controls that share the same DataSource.
Instantiate a new BindingContext and assign it to the BindingContext property of one of the ComboBoxes:
comboBox2.BindingContext = new BindingContext();
This should solve the problem.
The datasource is a separate object. When one of the controls changes the datasource active row it sends out an update notification to the other controls to move accordingly. This is normal and expected behavior.
The idea behind it is to simplify navigating record sets while keeping all the bound controls in sync.
If you don't want that, use two datasources tied to the same underlying data.
I think that might be intended to be a feature. For Master/Detail type forms.
I have a small usercontrol that basically increments or decrements a value by one. The user control has two buttons(one to add and the other to subtract) and a textBlock that is used to display the value.
I am going to have multiple instance of this usercontrol in another usercontrol so I can manipulate values of a dataclass that has an INotifyPropertyChanged interface. My question is how can I databind the textBlock of the value changing usercontrol to the usercontrol I instansiated it in?
First, I want to state that Silverlight 2 does not support element to element binding. That feature is added in Silverlight 3 (out in Beta now). Having said that, I don't think you want to bind controls together anyway. It sounds like you're trying to build a NumericUpDown control and you probably have some class in code behind that's actually doing the incrementing and decrementing.
If that's the case, you can simply subscribe to the click handlers and call a method on your model like Increment or Decrement. Your model can expose a property for the current value and that property is what is bound to your text box.
Now if you're actually trying to build a NumericUpDown control, you might want to check out the Silverlight Toolkit. The toolkit already includes this control and it also supports data binding.
Check out the NumericUpDown Control here and download the toolkit here.
Finally, binding from a child control to a parent control really isn't any different. The parent UserControl has a DataContext and all child controls inherit that. Each individual child control can also have its DataContext set. Binding expressions are always relative to the DataContext and the DataContext can be set in code. In your case, probably to a model of some sort.
I hope that helps.