I'd like to create a re-usable ListView with rows of any or all of:
Labels + textboxes
or
Labels + Comboboxes
or
Labels + DatePickers
using Templates. I still do not understand Templates too well and would like to know which of them - ControlTemplate, DataTemplate, ItemsTemplate or ContentTemplate - to use for this and how. Thanks!
There are 2 different types of templates: DataTemplate and ControlTemplate. ControlTemplate is used on the Template property of classes derived from Control and defines the visual tree for a specific type of control.
Pretty much any other place that templates show up is using DataTemplate. This includes ContentTemplate and ItemTemplate properties. DataTemplates define a visual tree for any non-Visual data type. When the template is rendered its DataContext is the data object being rendered (i.e. a List<T> item) making it easy to bind data properties.
To mix templates for different types in a single list you can use a DataTemplateSelector which allows you to write code to pick a template for each item. The other option is to create multiple implicit templates (DataType but no x:Key) for the different CLR types of objects in the list. As long as those templates are in the resource scope of the control rendering the collection the types will resolve their templates automatically.
Related
I implemented some generic CustomControls in WPF, for instance an AutoCompleteTextBox.
Now, I'd like to implement a generic ViewModel library, in order to perform the databind of these controls.
Now I defined one attached property named CDataSource, that specifies the source of the data to bind within the control.
My question is : Is it possible, that the CustomControl passes to the ViewModel the CDataSource value? In this way the ViewModel may populate the control on the basis of the CDataSource property.
Thanks in advance
This seems like a strange request to me. You don't want any dependency on your view model from within your custom control. Instead, you would normally have a dependency property on your custom control which is the ItemsSource, and then you would set the value of this from your view in XAML.
This is how the AutoCompleteBox included in the WPF Toolkit operates.
I have a UserControl without Content, because the control which should be shown inside of the UserControl is created at runtime. I would like to solve this like follows, but don't know how to implement it:
Create a Control-variable in the ViewModel
Set it at runtime when the content is created
Bind a content property (inside the UserControl) to that variable
The problem is, that I don't know how to bind to the control-variable.
Why just not to use ContentControl instead of UserControl and provide Content in runtime by introducing a DataTemplateSelector which able to provide right DataTemplate in runtime?
You can encapsulate your Content-area controls in DataTemplates and select appropriate one in runtime.
I've like 'base' DataTemplate that contains TabControl with 3 tabs. In each tab I put empty ContentPresenter.
I want to write new DataTemplate for each derived type. In which, I want to populate the all 3 ContentPresenter.
How can I reffer to each ContentPresenter of the base template so I can put data inside?
I assume you have set content of each tabitem.
If the content objects have different types you just have to make a datatemplate for each of the types.
I'm creating a WPF custom control as an auto learning exercise. My control has a ListView inside the template. I wanto my control user be able on defining the needed columns in his own Xaml, but I did not get the strategy on how to pass the columns to the inner listview since binding with FindAncestor complain that "Columns" is not a DependencyProperty.
Wekk the questions are:
How to achieve bind a property from xaml to the template when it is not a DP
Correct my design: I think there is something wrong: if someone would change completely my template, how should I let him use the Column collection ?
why not inherit from ListView directly? Then you have all the properties you need for the ListView and can also add you own properties to the class.
Then you can apply a custom Style to your control to make it look like you want. (Here you have a basic ListView Style that you can use and expand to your needs)
Sometimes binding to a property that is not a dependency property can be solved using the Binding Mode OneWayToSource
Have you tried that?
I have a WPF user control that will dynamically have any number of GridViews. Each is essentially the exact same except for the ItemsSource. Therefore each have the same columns and the same RowDetailsTemplate. To be specific what I am actually doing is settings the columns to the bound values and then setting the RowDetailsTemplate to the data template.
What is the best approach to essentially define the GridView once and copy it to all the others. I have tried XamlWriter.Save with the GridView in the resources, but the columns and RowDetailsTemplate are not saved. I have also tried a style, but the columns property is not settable.
Maybe I did something wrong with the two approaches.
Use an ItemsControl with ItemTemplate. The DataTemplate used for the Itemtemplate should contain the GridView XAML with all the column definitions. Next bind the ItemsControl's ItemSource to a collection of whatever data object you want the GridViews to use as ItemSource.