Is it possible to change value from xaml? - wpf

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.

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.

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.

Expose an inner depedency property to the main user control

I am working in silverlight.
Made a new UserControl called TextBoxWithButton.
Now i want add a new property to my new control called TextBoxBackground.
I did this :
public partial class TextBoxWithButton : UserControl
{
public Brush TextBoxBackground
{
get{return textBox.Background;}
set{textBox.Background = value;}
}
}
This works fine, but when I try to animate this property I get an exception.
I think it's because TextBoxWithButton should be defined as a dependency property but I don't know exactly how to to this.
You need to turn this into a Dependency Property. For details on implementing a DP, see Custom Dependency Properties.
Once you have this setup as a Dependency Property, just bind your (inner) TextBox.Background to the "local" TextBoxBackground property (in xaml). You can then animate the UserControl's TextBoxBackground property as needed, and the "inner" property will change as well.

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.

Resources