WPF "Insert" control per binding - wpf

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.

Related

WPF: How can I retrieve the Template that was created by a ContentTemplateSelector for a specific ContentPresenter?

In my WPF app, I'm using several ContentPresenters with a special MarkupExtension that requires access to the ContentPresenter's ContentTemplate property.
The MarkupExtension works very well, except that I just found out that if a ContentPresetner uses a ContentTemplateSelector, it doesn't set its own ContentPresenter property: rather, both the ContentPresenter and the result of the ContentTemplateSelector get saved to a private variable of the ContentPresenter class, as can be seen here: link to .Net source code for ContentPresenter.
I figured out that I can call ContentTemplateSelector.SelectTemplate() again and get the template, or keep a dictionary of selected templates inside the ContentTemplateSelector so that I can fetch the template that was geneatedfor each element, but is there a better way to do this?

ListView Custom Control Template reset when adding view

The control resets to the standard ListView in the designer when I declare a view in XAML.
<my1:CustomControl1.View>
<GridView></GridView>
</my1:CustomControl1.View>
I tried doing the same thing in code. The CustomControl1.Template changes from having a TargetType of "CustomControl1" to the default ListView template after I set the view to a new GridView().
The template itself doesn't seem to be the problem - it works if I use it as a local resource. If I can't get this working I could make a UserControl with a templated listview inside but for various reasons I'd like a CustomControl. Any help appreciated.
I found another solution here. You create your own GridView class and have it return the custom control as the default style key. You then use this instead of the standard gridview
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/056df061-0666-4fe2-9fa6-8a6440a23ff0/

Generic ViewModel that works with ItemsControl

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.

WPF binding to a non dependency property on a control template

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?

How can I dynamically "bind" a class to a different xaml?

Let's say I want to switCh between 2 UserControls programmatically:
one UserControl contains a TextBox with a label
the other contains only TextBox
I'd like a single codebehind. Is it possible ? if yes how ? If not how to do the above requirement ?
If I understand you right, you have two usercontrols acting similar and you don't want to write the code-behind double?
Create a ViewModel for the controls. Use for both of the controls the same ViewModel. Look at the MVVM-Pattern.
Another possibility would be to create a CustomControl and make two templates.
Why not have one usercontrol and switch the Visibility of the Label using a style trigger based on some property of the datacontext?

Resources