.NET – DATATEMPLATE.DATATYPE in Silverlight

I have a view model with an attribute, which is an ObservableCollection. In the view that uses this attribute, I have an ItemsControl as follows:

...

...

FieldVM is an abstract class, It is implemented by classes such as TextFieldVM and EnumFieldVM. During runtime, these FieldVM implementations are added to the Fields property, and I want them to display the views associated with them in my view.

In WPF, this is very simple , I always do this. You just need to do this in the appropriate resource dictionary, and everything works as expected:







Now, Silverlight works for the first time, I wish I could do the same thing, but the DataTemplate does not have a DataType attribute. I am stuck What is Silverlight’s approach?

Use a value converter to bind the type to the visibility of each view:

< /p>

 

Visibilty="{Binding Converter={StaticResource ViewVisibilityConverter}, ConverterParameter=Enum }" />
Visibilty="{Binding Converter={StaticResource ViewVisibilityConverter}, ConverterParameter=Text}" />

In the ConvertTo of ViewVisibilityConverter, the visibility is switched according to the type.

Another way to view is to use different types of value converters to return different data templates from Application.Resources.< /p>

In ItemTemplateFactory.Convert():

var fieldVM = value as FieldVM;

switch fieldVM.FieldType:
{
case "Text":
return Application.Current .Resources["TextTemplate"] as DataTemplate;

case "Enum":
return Applica tion.Current.Resources["EnumTemplate"] as DataTemplate;

}

I have an attribute view model, which is an ObservableCollection< FieldVM>. In the view that uses this attribute, I have an ItemsControl as follows:

...

...

FieldVM is an abstract class, implemented by classes such as TextFieldVM and EnumFieldVM. During runtime, these FieldVM implementations are added to the Fields property, and I want them Show the view associated with it in my view.

In WPF, it’s easy to do this, and I do it all the time. You just need to do this in the appropriate resource dictionary, and everything is as per It works as expected:







Now , Silverlight works for the first time, I hope I can do the same thing, but DataTemplate does not have a DataType property. I am stuck. What is Silverlight’s approach?

Use the value converter to bind the type to the visibility of each view:

 

Visibilty="{Binding Converter={StaticResource ViewVisibilityConverter}, ConverterParameter=Enum}" />
Visibilty="{Binding Converter={StaticResource ViewVisibilityConverter}, ConverterParameter=Text}" />

In the ConvertTo of ViewVisibilityConverter , Switch visibility according to type.

Another way to view is to use different types of value converters to return different data templates from Application.Resources.

In ItemTemplateFactory.Convert():

var fieldVM = value as FieldVM;

switch fieldVM.FieldType:
{
case "Text":
return Application.Current.Resources["TextTemplate"] as DataTemplate;

case "Enum":
return Application.Current.Resources["EnumTemplate"] as DataTemplate;

}

Leave a Comment

Your email address will not be published.