Data Template are only necessary in ItemsControl controls? - c#

Let me explain you my situation.
I have a base class called Shape, and several concrete classes like Triangle, Square, etc.
I have several data templates.
I'm building just one object. So I wouldn't use an ItemControl control, I would like to use a normal panel like the grid, and show the respective data template (in DataContext has the concrete item)..
The only way to do this is using an ItemsControl? Or there's another way.. because I'm just using one item and not a collection and display the correct template.

DataTemplates are used in much more than just ItemsControls
They are used to tell WPF how to draw any object in the Visual Tree. For example, if you stick a User class object in the VisualTree, a DataTemplate can be used to tell WPF how to draw that User object
They are most frequently used in controls with an ItemsSource or Content properties, because those are the most common way of inserting data objects into the VisualTree.
In your specific case where you only want to insert one data item into the VisualTree, I would suggest a ContentControl
<ContentControl Content="{Binding MyDataObject}" />
To tell WPF how to draw MyDataObject you can either use the ContentTemplate property and set it to a DataTemplate
<ContentControl Content="{Binding MyDataObject}"
ContentTemplate="{StaticResource MyDataTemplate}" />
or define an implicit DataTemplate that tells WPF to draw any object of a specific type using a specific template.
<DataTemplate DataType="{x:Type local:MyDataObject}">
<!-- Tell WPF how to draw MyDataObject here -->
</DataTemplate>

If you want to display a single item with a data template that is selected based on the item's type, you should use ContentControl or any of its derived classes.

Related

Put different kinds of user-controls in a wpf grid

I'm starting with WPF, and I want to create a grid which shows shapes according to the context. So for example in one cell, I can have either a circle, or a rectangle !
I created my custom circle "Circle.Xaml" and my custom rectangle "Rectangle.Xaml". I created also their View-Models "CircleVM.Cs" and "RectangleVM.Cs".
Now, I added a listBox to my application to let the user to put either the circle, or the rectangle in my grid cell.
My problem is:
How can I bind all that with my cell ? In order to put the right control when I'll add simply his corresponding View-Model in the collection binded to my grid !!
How can I set the DataContext of a view when the view-Model constructor has arguments which are not defined at the initialisation (Values of arguments are according to the context too) !!
When you declare a DataTemplate in a Resources section, you have a choice... you can provide it with a name and then use that name to reference it, or you leave it without a name. If a DataTemplate has no name (or x:Key value) then it will automatically be applied to all objects of the type specified in it unless they have another DataTemplate explicity set with a named DataTemplate.
Therefore, you can add the following into your application Resources section (in App.xaml) and it will apply to all of the view models in your application:
<DataTemplate DataType="{x:Type ViewModels:CircleVM}">
<Views:Circle />
</DataTemplate>
<DataTemplate DataType="{x:Type ViewModels:RectangleVM}">
<Views:Rectangle />
</DataTemplate>
Please note that you will need to add the required XML Namespaces... something like this:
xmlns:ViewModels="clr-namespace:YourApplicationName.FolderNameIfApplicable"
xmlns:Views="clr-namespace:YourApplicationName.FolderNameIfApplicable"
Now, whenever you add an instance of your CircleVM class into the UI, the related Circle.xaml file will be shown:
<ContentControl Content="{Binding YourViewModelProperty}" />
To switch between the two view models, create a property as above... again, you have two choices:
public object YourViewModelProperty // with valid getter/setter removed here
Or better, create a BaseViewModel class that implements the INotifyPropertyChangedinterface and extend all of your view models from it:
public BaseViewModel YourViewModelProperty // with valid getter/setter removed here
The final step to take when the user changes their selection is to set the value of this property:
if (userHasChosenCircle) YourViewModelProperty = new CircleVM();
else YourViewModelProperty = new RectangleVM();

WPF dynamic panel creation in MVVM

I am creating an editor for an object instance hierarchy. The editor has a panel whose child controls vary depending on the fields of the object. So for type A which has an integer field it will have a spinner control; for type B which has a string field it will have a TextBox. And so on.
Question is, how do you achieve this in MVVM?
You can use ContentControl in your XAML code nad bind to some type (event system types - what you want):
<ContentControl Content="{Binding YourProperty}"/>
(YourProperty is a Property from you ViewModel attached to view)
then you have to create DataTemplate that render view:
<DataTemplate DataType="{x:Type system:int}">
<views:MyWindow/>
</DataTemplate>
system and views are namespaces in you xaml code. In this example I use system:int type, but it can be your custom type. MyWindow is an UserControl object - so you basically create another WPF UserControl file in your solution.
It works like this. ContentControl gets its Content and check it's type. It looks for DataTemplate that can cast this type to some View (it can be text, textbox, ect) that can be rendered as a Content.
Best regards

how to create binding in code when the datatemplate is declared in xaml

i have a datatemplate declared in xaml.
for e.g.
<DataTemplate x:Key="TestTemplate">
<StackPanel>
<TextBox Name="txtBox" Visibility="Visible"></TextBox>
</StackPanel>
</DataTemplate>
I wish to set the binding for txtBox in code behind before the element is generated because i have different binding paths for different elements that get generated
I can get the template in the code behind as :
DataTemplate tmplt = FindResource("TestTemplate") as DataTemplate;
but i am not sure what to do next. How to get the the txtBox reference to set the binding.
We have to remember one thing that Templates are not instantiated UI controls. They are streamed obejcts in XAML and are shared between UI elements. So if you edit a dataTemplate and change its stucture (by adding, editing, deleting an element under the template) it would change the one data template which is shared among controls. Thus other elements using that template will also be affected by the change.
Now lets address your issue of adding a dynamic biding to a textbox. You say each generated textbox will have different binding paths. So this definitely does NOT call for changing the data template itself!
You will have to access the text box and add dynamic bindings to it AFTER the textbox's is generated.
I see that your binding differs based on your "situation", so why cant you use TemplateSelector? Template selector will decide which data template (having one specific binding applied to the TetxBox) at runtime.
The first part of answer - is FindName() method.
example:
DataTemplate tmplt = FindResource("TestTemplate") as DataTemplate;
TextBox my = (TextBox)tmplt.FindName("txtBox");
try out this, it should help to get access to TextBox control. I think that you know how to bind to. If you want your DataBinding behave different way, use MultiBinding and Converter.
EDIT
public class GeneralObject
{
private object someObject;
public GeneralObject(object initObject)
{
this.someObject = initObject;
}
//If you want to bind to some text, for example
public string Text
{
get
{
//I think you know which objects are coming as input
if (this.someObject is SpecialClass1)
return ((SpecialClass1)this.someObject).SpecialClass1TextProperty;
if (this.someObject is SpecialClass2)
return ((SpecialClass2)this.someObject).SpecialClass2TextProperty;
//and so on.
}
}
}
EDIT 2
One more possible way
So I remember, that WPF have ContentControl!
<ContentControl Content="{Binding Path=CurrentObject}"/>
But in this case you have to create number of DataTemplate's, every Template for one class.
<DataTemplate DataType="{x:Type local:SpecialClass1}">
...
</DataTemplate>
<DataTemplate DataType="{x:Type local:SpecialClass2}">
...
</DataTemplate>
<!--and so on-->
WPF resolve DataTypes of ContentControl.Content property, and put to the ContentControl right DataTemplate.

WPF Custom Control - ItemsControl template not being applied

I'm building a custom WPF control that derives from TabControl. In the ControlTemplate, I'm using a ItemsControl to display a list that is being bound from the template (an observable collection of type FileMenuItem). During program execution, I'm getting the following error in the output window:
ItemTemplate and ItemTemplateSelector
are ignored for items already of the
ItemsControl's container type;
Type='FileMenuItem'
The type FileMenuItem is derived from MenuItem. If I change the base class to DependencyObject, the code actually runs and the template is applied (so that's an option). I googled the error and couldn't find anything about it, has anyone run into this while developing custom controls? Even though I have a workaround, I'd like to understand what's happening, and I think using the MenuItem as a base class is a cleaner implementation.
I can post more code if it would help. Thanks!
The purpose of a DataTemplate (like ItemTemplate) is to provide a visualization for a data object. Specifically, it defines a set of elements to add to the visual tree in place of the data given to an ContentPresenter or ItemsPresenter. In your case your source list is a collection of objects that are already able to be added directly to the visual tree for display in the UI.
You can see this in the following simplified example where only "Three" shows up in Red because the first two items are defined in a form that can be displayed directly by ComboBox.
<ComboBox>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Foreground="Red"/>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBoxItem>One</ComboBoxItem>
<ComboBoxItem>Two</ComboBoxItem>
<sys:String>Three</sys:String>
</ComboBox>

How do build a VS-like propertypanel in WPF?

I got a TreeView containing different objects from different classes. Now I want to build a propertypanel, which shows up different content, depenting on what object/class is selected in the TreeView. What is the best way to build such panel? Differnt panels and collapsing panels depending on the selection(Whould make implementing the ObserverPattern this easier for me?)? Or an other approach?
I would bind the property panel (which could be just a ContentControl) to the SelectedItem in the TreeView:
<ContentPanel Content="{Binding SelectedItem, ElementName=_treeView}"/>
Then I would use DataTemplates to show the correct panel for each class of item you have:
<DataTemplate DataType="{x:Type local:SomeClass}">
<Label>This is displayed for SomeClass</Label>
</DataTemplate>
<DataTemplate DataType="{x:Type local:SomeOtherClass}">
<Label>This is displayed for SomeOtherClass</Label>
</DataTemplate>
Obviously your DataTemplates can be as complex as needed to display the various classes present in the TreeView.
Do you mean a property grid?

Categories

Resources