WPF - UserControl default Content attribute - wpf

I'm creating a UserControl and I just can't remember the name of the attribute which you use to decorate the property which you want to act as the default content property.
To give a concrete example, say i have a property called 'Title' which i can set using property syntax like this -
<local:myControl Title="the title"/>
But the consumer of the control may want to use element syntax like this -
<local:myControl> the Title </local:myControl>
I KNOW there is an attribute which I need to add to the Title property with to enable this support but I've forgotten what it is and can't find it anywhere.
Could anyone refresh my memory for me? Also, I'm looking for a similar attribute to act on CustomControls inheriting from ItemsControl.

ContentPropertyAttribute

I also found the code for supporting collections as the content property on MSDN. TOM_C is to thank for this.
[ContentProperty("SomeObjects")]
public class SomeContainer
{
private List<SomeObject> _someObjects;
public List<SomeObject> SomeObjects
{
get
{
if (null == _someObjects)
_someObjects = new List<SomeObject>();
return _someObjects;
}
}
}
XAML:
<SomeContainer>
<SomeObject/>
<SomeObject/>
<SomeObject/>
</SomeContainer>

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.

SortDescription with custom attached property

In Xaml I can set a custom attached property using
local:TestClass.TestProperty="1"
An I can bind to a custom attached property using
{Binding Path=(Namespace:[OwnerType].[PropertyName])}
{Binding Path=(local:TestClass.TestProperty)}
But how do I specify the namespace when I need to use a custom attached property in a SortDescription?
I can bind to an attached property using
new SortDescription("(Grid.Row)", ListSortDirection.Descending)
but here I can't set a namespace anywhere...
Best Regards,
Jesper
You can't, for the same reason that {Binding Path=a:b.c} works but {Binding a:b.c} doesn't: The PropertyPath constructor has no namespace context.
Unfortunately in the case of SortDescription there isn't much you can do about it. You have to find a way to sort without using attached properties.
Normally I tell people that use of Tag is an indicator of bad coding, but in this case Tag may be your best option: You can create an object within Tag that has properties that return the actual attached properties you want.
In your PropertyChangedCallback, instantiate Tag to an instance of an inner class:
public class TestClass : DependencyObject
{
... TestProperty declaration ...
PropertyChangedCallback = (obj, e) =>
{
...
if(obj.Tag==null) obj.Tag = new PropertyProxy { Container = obj };
});
public class PropertyProxy
{
DependencyObject Container;
public SomeType TestProperty { get { return GetTestProperty(Container); } }
}
}
Now you can use the sub-property of Tag in your SortDescription:
<SortDescription PropertyName="Tag.TestProperty" />
If there is only a single property to be sorted, you can simply use the Tag for it.
The main problem with this is that using the Tag property will conflict with any other code that also tries to use the Tag. So you may want to look for some obscure DependencyProperty in the standard libraries that doesn't even apply to the objects in question and use that instead of Tag.

Binding an Element to a Control Property (string)

so, i've found a way to bind a label to a property on current Control
i give it a name:
<UserControl x:Class="WpfGridtest.GridControl" x:Name="GridControlControl1">
and than bind to property of this control:
<Label Content="{Binding ElementName=GridControlControl1, Path=Filter}"></Label>
I can see the default value i put in that property.
I am guessing that this isn't working because i am binding to String property which doesn't implement INotifyPropertyChanged??
is there some other type i should be using for this property instead of String auto notify my label of changes, or am i going about this the wrong way?
The INotifyPropertyChanged interface should be implemented by the class that contains the property - in this case, by your WpfGridtest.GridControl.
Also, if you want to use your properties for UI, consider using a DependencyProperty as a storage instead of a private field.
in addition, it is also likely that the default binding mode is one time, so you may have to change it in your {Binding}

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.

Resources