Get textbox binding in wpf datagridtemplatecolumn - wpf

I have a wpf datagrid containing multiple datagridtemplatecolumns, which all are build up from a datatemplate which contains a textbox.
Now I want to get the binding of the textbox (I have a reference to the template column which textbox's binding I woukld like to determine).
Alternatively it would be nice to return the X:Name of the template column
Any hints?
Regards
klaus

Use VisualTreeHelper to travers the grid and find all text boxes (e.g. by name) and then use GetBindingExpression to get the bindig: control.GetBindingExpression(control.TextProperty)

Related

Issue : ListBox hides all the stack panel and text boxes with in them, Wpf

I want to bind my textboxes in wpf xaml. That is doing perfectly but when i get textbox with his x:Name="myTextbox" in codebehind. He says
"The name 'myTextbox' does not exist in the current context"
Using WPF, you cannot access items by name if there are inside a DataTemplate, like in your case, probably. However is not a good practice to modify the template using code behind. If you want to use correctly WPF, take a look at MVVM pattern.
In your case, the ListBox should be bound to an ObservableCollection and inside the template you can bind your textbox or whatever you have to the Item object.
Don't access the textbox like this : x:Name="myTextbox"
Instead access it like myTextbox.Text = "Hello World";.

datarowview binding to combobox datatemplate

I have a wpf application using mvvm. I am trying to bind my datatable to a combobox that has a datatemplate of 2 textblocks. How do I specify the binding on these text blocks to bind to datarowview.item["columnname"]
In short, what I am trying to achieve is to bind 2 columns of my datatable to a multicolumn combo box
It turns our that you can simply bind to the column names just like how you blind to an object's public property. WPF is smart enough to figure it out.

Any ideas how to implement ComboBox to represent hierarchical data?

Does anybody saw control like this somewhere?
I need to make such control to represent hierarchical data (it should be generic very likely, i.e. data binding, templates support).
Something like combination of ComboBox and MenuItem’s.
I think I will redefine the combobox itemtemplate with some hierarchicaldatatemplate along with popup class.
Just put ComboBoxes on a form and bind the ItemsSource to the top level collection.
Then bind the DataContext of the next ComboBox to the SelectedItem of the box on the left and bind its ItemSource to the collection of items.
You know how to bind to SelectedItem?
E.G.
Column1
Public String Name
Public List Column2s
So you bind the first combox to List with he displaymemberpath = name
Then on the second combobox you bind to Column1 selecteditem with items source path of Column2s
The trick is to build up the Lists within the Lists within the Lists
All right, I made it by custom control inherited from ComboBox, custom ComboBoxItem inherited from HeaderedItemsControl and using HierarchicalDataTemplate.

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?

WPF control with many GridViews

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.

Resources