Custom control Tag - silverlight-4.0

I am designing the custom control and there is a requirement as :
<Control:MyControl>
<Control:MyControl.Property1>
<Control:MyControl.Property2></Control:MyControl.Property2>
</Control:MyControl.Property1>
</Control:MyControl>
Can you please suggest me the solution for this ?
Thank You

Simply implement the properties in your control as full properties, or even just auto-properties like this :
public object IAmAProperty {get; set;}
You can now define a value for this property from XAML :
<control:MyControl>
<Control:MyControl.IAmAProperty>i am a value</Control:MyControl.IAmAProperty>
</control:MyControl>
However i should warn that this:
<Control:MyControl.Property1>
<Control:MyControl.Property2></Control:MyControl.Property2>
</Control:MyControl.Property1>
is not going to work . (Property2 of MyControl cannot be nested inside Property1 of MyControl)

Related

WinrtXamlToolkit TreeView ItemContainerStyleSelector not firing

I am using the TreeView control from the WinrtXamlToolkit in a uwp app. I want to apply a different style to some TreeViewItems depending on a conditon so I created a class, TreeViewItemStyleSelector which derives from StyleSelector and I override the SelectStyleCore function.
public Style ResourceStyle { get; set; }
public Style ClassroomStyle { get; set; }
protected override Style SelectStyleCore(object item, DependencyObject container)
{
// control never reaches here.
// logic to apply style
}
Then in xaml I use it like this.
In Page Resources
<StyleSelectors:TreeViewItemStyleSelector ResourceStyle="{StaticResource AStyle}" ClassroomStyle = "{StaticResource BStyle}"/>
And later in the page.
<wxtControls:TreeView ItemsSource="{Binding StructureViewModels}" ItemContainterStyleSelector="{StaticResource TreeViewItemStyleSelector}" />
The problem is that the SelectStyleCore override is never called. Does anybody know why?
I am not yet sure what's the reason this doesn't work, although I have some theories. One is - this was never implemented. Perhaps at least at the root level it should work because it's an ItemsControl, but because of the way it's implemented (hierarchically) - the ItemContainerStyleSelector would have to be forwarded from the TreeView to the TreeViewItems, which it isn't.
I haven't had a chance to try to reproduce it yet, but if I were to try to fix it or work around it - I would first try forwarding that property in HeaderedItemsControl.cs - roughly where it says "// Note: this is where we would apply the HeaderTemplateSelector (...) (if implemented)". The alternative (if you don't want to modify the toolkit's code) might be to specify the template for the TreeViewItem and in the template - use a StyleSelector on template parts you want to be different for different data items.

How do I add a resource as the description attribute of a dependency property for localisation

I have a dependency property in a custom control which has the following attribute to enable the property to appear under a specific category in the properties viewer with a relevant description.
<Category("Vtl Button Images"), Description("Add an image of your choice or leave blank to utilise the default image supplied.")>
Public Property SaveImage As ImageSource
Now I would like to localise the description element of the attribute so I added a resource string to the project and attempteted to reference it like so.
<Category("Vtl Button Images"), Description(My.Resources.ImageDescription)>
Public Property SaveImage As ImageSource
However this throws an error telling me that a constant expression is required. I get the same error if I try and create a constant set to a resource.
So is it possible to localise the description element of attributes like this and if so how?

How do I bind to public methods with xaml and codebehind?

I am setting the DataContext property of my xaml file in codebehind like this.
DataContext = _systemObject;
I want to bind a listview with a public method of the _systemObject that returns a "List" called ListPeople().
How can I do this? do I have to create a property that wraps the ListPeople() method inside _systemObject?
Thank you for reading...

fluent nhibernate polymorphism. how to check for type of class

I have an Icon which has a Content (one to one) relationship.
public class Icon
{
public virtual Content Content {get; set;}
}
By default, it is lazy loaded which is what I want.
However, at some point in the code, I need to check what kind of Content is, Content being polymorphic, something like
if(icon.Content is TextContent)
{
...
}
Icon is part of another association and it is automatically obtained by the NHibernate, I do not get it manually.
What is the recommended way of checking for the actual type in this situation?
I can have a specific property like ContentType which will be an enum in order to identify the actual content type, but I am looking to know if there's a different way.
If you want to do that kind of check, you have to remove the proxy from the property.
There is a few ways to do it:
If you have access to the session call:
session.PersistenceContext.Unproxy(icon.Content);
Implement a virtual method (in a base class if possible) that forces the removal of the proxy by returning the instance with the proper type.
public virtual U As<U>() where U : YourType {
return this as U;
}
Disable the lazy initialization of the property.
This is very similar to another recent question.
To add to csanchez's list, a fourth method is to add a Self property to the Content base class that returns the un-proxied type:
public virtual void Self
{
get { return this; }
}
And a fifth method is to use 'lazy="no-proxy"` in the mapping as described on Ayende's blog.
Thanks for the suggestions but meanwhile I found an interesting solution, better I think.
Using the Visitor pattern, I can define an IconContent visitor and pass an Action to be executed to it.
For example, suppose there is a TextContent and an ImageContent, it will be something like this:
IconContentVisitor.Func(()=> { Console.WriteLine("this is TextContent"; }, ()=> { Console.WriteLine("this is ImageContent"));
Idea came from here: http://mookid.dk/oncode/archives/991

WP7/XAML: data binding to a property in the code-behind file

I'm rather new to XAML and Silverlight. I have a XAML page and a code behind class for it. In the class, I have a protected read-only property. Can I bind a control to that property? Trying to specify the root element of the XAML as the DataContext (by name, as an ElementName) causes a designer error "Value does not fall within the expected range."
EDIT: I'd like to do in the designer-fiendly way. I understand I can do everything (including control population) from code; that's not the point. Can I have the designer recognize and display the properties of my code-behind class? Not one ones from the base (PhoneApplicationPage) but the ones that I define?
Your code behind should be the datacontext.
For example on a main page code behind:
public MainPage()
{
InitializeComponent();
DataContext = this;
}
You should be able to bind to the protected property but only one way ie from the property to the xaml. As it is read-only you will not be able to get the value if it is changed on the page by the user.