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

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}

Related

How to set my custom class's member View's property from XAML? (Xamarin.forms)

I'm making app with using Xamarin.forms.
I already asked question here.
How to set child of class' property with using xaml? (Xamarin.forms)
But I couldn't get right answer for this, or there may be no solution for that.
What I want to do is setting my class's view's property from ContentPage's XAML.
my class has some view like Image and else.
I searched and found that there is 'ControlTemplete'. But I'm not sure it's what I'm looking for.
And I also don't think putting BindableProperty and OnPropertyChangedDelegate codes for every property that I want to set is a best way.
Is there another better solution?
Thanks.
You can map XAML that is inside your control to a property using ContentProperty attribute.
[ContentProperty("MyContent")]
public class MyControl : ContentView
{
public View MyContent { get; set; }
}
And in XAML somthing like this
<local:MyControl>
<Grid></Grid>
</local:MyControl>
this limits you to only one property but should work with any types.

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.

Silverlight databinding question

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.

How to update binding dynamically when source object changes?

What I have?
I have Frame in XAML, (binding works properly).
<Frame Name="frmMainContent"
DataContext="MyPageInformation"
Source="{Binding ElementName=thisPage, Path=MyPageInformation.UserControlPath}"
NavigationUIVisibility="Hidden"></Frame>
In the code behind I have a DependencyProperty of my class, PageInformation
public static DependencyProperty PageInformationProperty = DependencyProperty.Register("MyPageInformation", typeof(PageInformation), typeof(Administration));
public PageInformation MyPageInformation
{
get { retur n (PageInformation)GetValue(PageInformationProperty); }
set{ SetValue(PageInformationProperty, value); }
}
What I want?
The Frame should update its binding whenever value of MyPageInformation changes.
Can somebody tell me how I can achieve this?
Thanks in advance!
You don't have to make the PageInformationProperty a dependency property just for this binding. Implement INotifyPropertyChanged in the code behind.
Also since you are actually binding to "UserControlPath", make sure that this property actually sends change notifications.
Well, the first thing I'll tell you is that all binding errors appear on the Output window. So you need to look at it and find out if you have any errors.
The next thing.. for bindings to update automatically, you either need to make the property it is "binded" to, either a dependency property, or implement INotifyPropertyChanged for other classes.
Be sure that the property that you exactly bind to, is either one of these cases.

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