WPF binding to a non dependency property on a control template - wpf

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?

Related

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 "Insert" control per binding

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.

WPF Custom Control - Binding a template item to a Path

In a WPF Custom Control template, is there any way that I can do the following in XAML?:
var selItemText = this.GetTemplateChild("PART_SelectedItemText") as TextBlock;
var binding = new Binding("SelectedItem." + DisplayMemberPath);
binding.RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent);
selItemText .SetBinding(TextBlock.TextProperty, binding);
Note that the interesting part of this statement is the binding constructor - I am building up a path based on both some text I specify ("SelectedItem."), and the path provided by the user.
The consumer would use the control similar to:
<c:MyControl DisplayMemberPath="Description" />
short answer: no, it's not possible to get this entirely in xaml within the controltemplate
your possibilities are:
use what you have (possibly using attached properties / a behavior to make it more MVVM-like)
use a MultiBinding one binding to the "SelectedItem" the other to "DisplayMemberPath" and your MultiValueConverter using Reflection to reflect down the DisplayMemberPath (may be a bit ugly)
create a class that inherits from Binding and exposes Properties that you can bind the DisplayMemberPath to and changes the underlying Binding (read here for how you can do this) (complicated)
use Reflection to instantiate a MS.Internal.Data.DisplayMemberTemplateSelector / build something similar
think about if your design is right. Other than your Control being some kind of ItemsControl (if that was the case you should inherit from ItemsControl and use the DisplayMemberPath you get there), I don't see why you shouldn't use a Binding on the outside like <c:MyControl DisplayMember="{Binding SelectedItem.Description}" /> and in your ControlTemplate use a TemplateBinding to bind to "DisplayMember"
You can split it to two different bindings. Have SelectedItem binds to a toplevel control of TextBlock and TextBox.Text bind to DisplayMemberPath as TemplateBinding.

WPF USer Control viewmodel binding

HI Can any one suggest me how bind viewmodel to a usercontrol..
Also please share different way of doing that..
I have added viewmodel and view into my xaml file in namespace and in the user control resource tag.. i have defined a data template with data type as the viewmodel wh i have wrote.. inside that i have added my view (i mean the same usercontrol ih which im editing now is it possible -- please let me know).. I have used content control with content={Binding}.. and contenttemplate as a datatemplate.. in that i have reffered the property which i want to bind from viewmodel).. but its not binding as such..
My query is different ways of binding viewmodel to view in UserControlLibrary Project ?
Sdry's right, you can also set the DataContext property of your view to your ViewModel.
Also, here's a WPF databinding cheat sheet that will likely come in handy: http://www.nbdtech.com/Free/WpfBinding.pdf
Set the datacontext of your view, with an instance of your viewmodel. Throughout your view you can then bind to the properties of your viewmodel with "Path" in the binding.

Resources