Silverlight databinding question - silverlight

Let's say I have a Class called ModelBase
public class ModelBase
{
public string Name
{
get { return "one"; }
}
}
and I have a property named Model of type ModelBase.
Now to the question how do I Bind to the Name property? The c# code would be this.Model.Name.
I've been trying to get this to work a long time, can some one enlighten me?

Not sure why you are having trouble with this.
You should be able to set the object that the Model property is on as the DataContext for your control, then simply bind using {Binding Model.Name}...
What have you tried to do so far?
(You can definitely bind to properties in Silverlight BTW)

You need to assign Model to the datacontext property before you can do any data binding, an example would be:
this.DataContext = Model;
In xaml, setup binding in this way:
<TextBlock Text={Binding Name}/>
Note: The way you declare the Name property only allows one time binding, to allow OneWay/TwoWay binding, look at dependencyproperty or INotifyPropertyChanged interface.

You can definitely databind to properties.
If you want more, you can use dependency properties of silverlight.
Check this URL.

Silverlight doesn't allow binding to properties. You'll need to expose a property on your viewmodel that returns the value of the models properties to bind correctly.

Related

Can I bind to a property of an instance of an object

Ok, I've looked around on here and on google and can't find an answer to this, or I'm just not understanding something correctly.
I have a class called PLCValues. In this class I have a property called TrkPresent.
In my viewModel, I create an instance of PLCValues called values. I want to bind to the TrkPresent property of the values instance. Is there anyway to do this? Still new to WPF and MVVM for that matter. Thanks for any help guys.
Okay, so I figured this out. When I created the instance of my object, in this case the values class. I had to create it as a property. So I said
public Values values {get; private set;}
then just declare it as normal in constructor of the viewModel:
values = new Values();
then when binding in xaml ensure that the DataContext is set to the viewModel containing the instance and bind to:
{Binding values.TrkPresent}
You should be able to bind to that property assuming the values instance is public:
TargetProperty = {Binding Path=values.TrkPresent}

DependencyProperty to ViewModel

I have two DependencyProperties TBLocation & TBBroadcastLocation in MyUserControl.
For TBLocation I get information from another element called MyTextBox.
When TBLocation gets set, I set TBBroadcastLocation with TBLocation's Point information.
And now I would like to make TBBroadcastLocation data available to MyViewModel so that it has TBLocation's data indirectly available to it.
How could I do that or do we have a better approach?
It seems to me as if you are asking 'How can I bind a property of a UserControl to a property of a view model'. You really should read the basics of data binding before asking these questions here. For future reference, please read the Data Binding Overview page at MSDN.
Given that you still have not provided enough information, I will assume that your property is of type string. In this case, your view model will need a standard property of type string to bind to your DependencyProperty... this property must implement the INotifyPropertyChanged interface:
private string viewModelProperty = string.Empty;
public string ViewModelProperty
{
get { return viewModelProperty; }
set { viewModelProperty = value; NotifyPropertyChanged("ViewModelProperty"); } }
}
Make sure that the DataContext of the Window that has your UserControl in it is set to an instance of the view model class:
In the MainWindow constructor:
DataContext = new ViewModelClass();
Or in XAML:
<DataTemplate DataType="{x:Type ViewModels:ViewModelClass}">
<Views:yourView />
</DataTemplate>
Then you simply bind with a Two Way binding:
<YourNamespace:MyUserControl
TBBroadcastLocation="{Binding ViewModelProperty, Mode=TwoWay}" />

Is it possible to change value from xaml?

Is it possible to change property value from xaml?
Imagine we have a usercontrol which have a property that is initialized already
public class MyUserControl : UserControl
{
...
public SomeClass MainWindow
{
get
{
return _someClass ?? (_someClass = new SomeClass();)
}
}
}
Now is it possible to change property of SomeClass without initializing it from xaml, and without animation?
Why xaml doesn't allow syntax to write <UserControl.MainWindow.Property>?
Add a setter to the property and allow the XAML to create its own SomeClass according to its need - that's now it is usually done.
XAML is declarative language, it doesn't try to be Turing complete or something like that, it merely describes creation of objects.
Of course, there is one extreme solution. But please, don't do it. For your sake, and everyone else's :)
EDIT:
Another solution could be to create a new property in the UserControl, and synchronize this property with property of SomeClass ( set{ this._someClass.someProperty = value; }). If you set this property in XAML declaration of the UserControl, the change will be propagated to the _someClass member.
Of course the wrapper property will have to be a dependency property, if you want to bind to the wrapped property.
One, you need to have a set to change the value. Two, just bind to it TwoWay in in the XAML.

How can I bind from a user control to an external object in XAML?

I have an image inside a user control that I want to bind it's visibility to a property I have set up in a class object. The dependency properties are set up and working correctly, but I don't know how to set the binding properly on the image.
The user control and class object are in the same namespace. I thought I would need to set the ElementName to the window or the RelativeSource to the class object, but I'm not getting it to work out.
Here's what a dependency property looks like (defined in MigrateUserWizardObject.cs, this inherits from DependencyObject, this resides in the UserAccountMigrator namespace):
public static readonly DependencyProperty DatabaseStepCompletedVisibilityProperty = DependencyProperty.Register("DatabaseStepCompletedVisibility", typeof(Visibility), typeof(MigrateUserWizardObject));
public Visibility DatabaseStepCompletedVisibility
{
get
{
return (Visibility)GetValue(DatabaseStepCompletedVisibilityProperty);
}
set
{
SetValue(DatabaseStepCompletedVisibilityProperty, value);
}
}
Here's an image that I want bound to this dependency property (defined in ProgressUserControl.xaml, this inherits from UserControl, this resides in the UserAccountMigrator namespace as well):
<Image x:Name="DatabaseCompleted" Source="{StaticResource GreenCheckMarkSource}" Visibility="{Binding Path=DatabaseStepCompletedVisibility, UpdateSourceTrigger=PropertyChanged}" Height="20" HorizontalAlignment="Right"></Image>
This is due to the fact that the DataContext of the image is the user control. How can I make this work?
I think you should look into using the Model-View-ViewModel pattern. Instead of setting the DataContext to the UserControl, set it to an instance of another class (ProgressViewModel, for example). This view model would have all the properties you want to bind to (including your DatabaseStepCompletedVisibility property) and makes it much easier. Right now you are wanting to bind some things to the UserControl, some things to another object somewhere else, etc.. and, as you have found, makes it difficult. Here is more information:
http://jmorrill.hjtcentral.com/Home/tabid/428/EntryId/432/MVVM-for-Tarded-Folks-Like-Me-or-MVVM-and-What-it-Means-to-Me.aspx
Without going that approach, you have to have an instance MigrateUserWizardObject to bind to. You can put that instance in your UserControl (if you insist on using it as the DataContext), then you can bind the the property of the MigrateUserWizardObject property of the UserControl. Also, your MigrateUserWizardObject doesn't have to be a dependency object or dependency property to bind to. A better pattern would be to make it a plain c# class that implements the INotifyPropertyChanged interface.

Dependency property in app.xaml.cs

I am new to WPF and the below question may look silly for many, please pardon me.
How can I create a dependency property in app.xaml.cs?
Actually, I tried to created it. The below code,
public static DependencyProperty TempProperty =
DependencyProperty.Register("Temp", typeof(string), typeof(App));
public string Temp
{
get { return (string)GetValue(TempProperty); }
set { SetValue(TempProperty, value); }
}
throws the below compile time errors:
The name 'GetValue' does not exist in the current context
The name 'SetValue' does not exist in the current context
Can anybody help me in this?
Thank you!
DependencyProperties can only be created on DependencyObjects, and since Application (which your App class inherits from) doesn't implement it, you can't create a DependencyProperty directly on the App class.
I assume you want this property to support binding. If this is the case, you have two options:
Implement INotifyPropertyChanged in App.xaml.cs
Create a DependencyObject derived class with your properties on it, and expose it as a standard read-only property of your App. The properties can then be successfully bound by "dotting-down" to them.
i.e if your new property is called Properties, you can bind like so:
<TextBlock Text="{Binding Properties.Temp}" />
If the property needs to be the target of a Binding, then option #2 is your best bet.
You class that contains dependency properties must inherit from DependencyObject.

Resources