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.
Related
I have the Property
ObservableCollection<List<AuditViewModel>>Audits
and i want to represent it in a xaml as a grid where the observableCollection represent the rows and the List represent the columns.
any idea how can i do it?
Thanks, Lin.
You can create a ListBox control and bind it's ItemSource to Audits. That gets you the rows. Then you could create an itemtemplate for the listview that displays all the values of the AuditViewModel. That ItemTemplate will be bound to AuditViewModel. Should just be "{Binding}".
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.
I've got a TabControl in my WPF application. The TabControl's ItemsSource is bound to an ObservableCollction of view objects. It uses a DataTemplate to populate the visual tree for the tabs from the Items in the collection.
I need to be select the current tabs in the conde-behind in response to actions the user takes on another screen in the application. When I iterate over the items in the TabControl's Items collection, I get the instances of my view models.
How do I access the actual TabItems and iterate over them, then select the one I want?
Tony
If you're using a MVVM approach you should bind your TabControl's SelectedItem property to the same object that holds your ObservableCollection of TabItems (the ViewModel). When you need to change the current tab set the SelectedItem property to the correct TabItem in the ObservableCollection.
I have a Usercontrol that contains a ListBox (lstClients) and a ComboBox
The ListBox has 2 DataTemplates setup as Resources called "LowDetailTemplate" and "HighDetailTemplate"
I need to be able to switch between the 2 DataTemplates when I change the value in a ComboBox from "Low" to "High" and vice versa. In the SelectionChanged event of the ComboBox I'm guessing I need to change the ItemTemplate of the ListBox but I'm struggling with the code to assign the DataTemplate in code behind. My latest attempt is shown below but it fails at runtime.
lstClients.ItemTemplate = (DataTemplate)this.Resources["LowDetailTemplate"];
It would be easier to define both of the views inside the same data template and then switch which is visible by use of a simple variable. Then your combo box change would just update that simple variable and so cause the view shown for each template instance to change.
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)