Passing objects to UserControl - wpf

I'm trying to create a CustomControl to which I can pass an "Person" object. How can I pass the person object to the custom control like this PersonDetails="{Binding Path=Person}" to the CusomControl ?
instead of passing Address="{Binding Path=Person.Address}"Address="{Binding Path=Person.FirstName}" ect

You have to declare PersonDetails as a dependency property.
Then you can use it as any other WPF control property - both in XAML (including data binding) and in code.

Related

How to create a dependency property in Silverlight using MVVM?

let say there is a textbox and i want to control the visibility of this control using MVVM, is there a sample on how to do this? First create a dependency property then get it hooked up in the ViewModel. Thanks.
Typically, you wouldn't need to use a dependency property in this case. Dependency properties really only need to be implemented for things like controls themselves, not for determining behavior. Behavior, such as the visibility of an element, can be handled directly via data binding.
Your ViewModel would just have some property, and you'd bind the TextBox.Visibility property directly to the ViewModel property.
The one "sticky point" is that you often will want to have some type of IValueConverter that will convert from your property type to a Visibility enum.

need to create binding and pass parameter in code behind

I need to pass a parameter to a binded command of a menuitem.click
the menu items are created dynamically in code behind, how can i create binding and a commandparameter in code behind?
Here is the answer: Binding Declarations Overview.
Basically you create an instance of Binding type and then assign it to an object via BindingOperations.SetBinding() method.

DataGrid binding Type issue

Here's my setup. I have the following BusinessObject classes
BaseClass
InheritClassA : BaseClass
InheritClassB : BaseClass
InheritClassC : BaseClass
I also have the following dictionary
Dictionary<classType is a String, ObservableCollection<BaseClass>>
I want to be able to, in my converter, base on the type of object I select, return the proper ObservableCollection so I can bind it to my datagrid, and autoGenerateColumn on so I can view all my properties on the grid.
However, when i just retrieve my collection back as BaseClass, it only shows me the columns for the base class. I think this is because the Type for the ObservableCollection is type of baseClass and not specific to InheritClassA/InheritClassB/InheritClassC
Is there a way to dynamically create a ObservableCollection's Type? So I can create ObservableCollection and assign casted values into it?
Is there a proper way to bind it to my datagrid so I can view the properties for the inherit class?
Thanks very much ,
Create a dictionary that has as the value-type an Object and not a BaseClass. Then you can add the concrete ObservableCollection<InheritClass[A,B,C]> and the binding will work like you expect:
Dictionary<classType is a String, ObservableCollection<object>>
If you want to be more restrictive, declare it as Dictionary<string,IEnumerable>

How do I link (dependency) properties in my ViewModel?

Simplified example:
I have an object that models a user. Users have a first name and a last name. The UserViewModel has a dependency property for my Models.User object. In the declaration of the UserView's xaml, I want to bind a couple of TextBlocks to the first and last name properties.
What is the correct way to do this? Should I have readonly DependencyProperties for the name fields, and when the dependency property User is set, update them? Can the name fields be regular C# properties instead? Or, should I bind like this:
<TextBlock Text="{Binding User.FirstName}" />
You typically will never use Dependency Properties in your ViewModel or Model classes. You'll want to have your ViewModel implement INotifyPropertyChanged instead.
If you do that, you can bind using the syntax above. (Though, if you want two-way binding to work appropriately, your "User" object will also need to implement INotifyPropertyChanged - otherwise, changes made in code to the user will not automatically reflect in the UI.)

Automatically creating child objects during Silverlight DataBinding?

I have a parent object of type Parent and it currently has a null property called Foo of type Child and that Child class has a property of type string called Name.
If the user types into a Text Box for that Name property then I want to automatically create an instance of Child and set it as the Foo property of Parent before finally setting the Name property of the Child object.
If i use:
{Binding parent.foo.name, Mode=TwoWay}
It doesn't create foo and essentially does nothing. Is there any way to achieve what I want without pre-creating all the possible child objects and then removing them if properties haven't been set?
There is no automatic way. You could consider using a pattern like M-V-VM and handling this logic in the ViewModel. You might also possibly get creative with an IValueConverter so that your binding can run custom code when the value is set. But WPF / Silverlight binding will not automatically do this work for you.

Resources