I have a grid in a XAML file and I want to generate its RowDefinition dynamically. To do that I create a List<RowDefinition> in which I add 3-4 RowDefinitions. Now I have to bind this property to the grid in the XAML file. How do I do this?
If you want to host dynamic content (varying number of items), the Grid control might not be your best option. Try using an ItemsControl (which can be templated to look however you want) and bind your actual data against the ItemsSource property. If you post some more information about what kind of content you are looking to display I can give you an example of how to do it with an ItemsControl.
Related
Is it possible to have a listbox displaying its items with a control template to change their look in the listbox and also have a canvas displaying the listboxItems with another look?
To be more accurate,
I have a UserControl named InfoControl and another named DesignerControl with the same DataContext: a DesignerVM which contains an ObservableCollection of CurveVM.
In my InfoControl, I have a listbox with its items binded to the OC and shown as stackpanels using a template.
Now I want to display my items in the DesignerControl (canvas) but with another look.
Should i have to create another listbox and synchronize it with the first one or is there a way to achieve my goal?
Two controls shouldn't share a listbox. Instead, make two separate listboxes and bind them to the same ItemsSource or DataContext. Use a DataTemplateSelector to control which template is used.
Edit: And welcome to StackOverflow :)
for an overview, an MVVM project in built in WPF.
Basically in my xaml, I have a datagrid bound to a dataview. When my service populates the dataview I get a dynamic table with an arbitrary number of columns.
For each column in the datagrid, I have created a headertemplate which contains a combobox which is bound to an Observable<Dictionary<string,BusinessEntity>> object as its item source in the xaml. Figuring out the combobox is another issue but I am trying to just populate data grid first, then worry about binding the combobox correctly.
anyways the only solution I have somewhat though of was to turn on autogeneratecolumn and then replacing all the headers with a combobox in the codebehind, but then I have issues trying to bind the combobox in the codebehind correctly and it doesn't feel MVVM if I have to create all those comboboxes there.
You can handle the AutoGeneratingColumn event to customize the auto generated column's header template.
I know I did this in Silverlight...
In WPF 4 I am trying to put controls in a DataGridTemplateColumn element in DataGrid XAML
I can't add anything to it and I get errors saying it doesn't support direct content. WTF?
I want to put in maybe a hyperlink, textboxes, a button, etc. I can't even add a canvas.
DataGridTemplateColumn uses Templates, as in DataTemplate, to define the content that should be created for the view and edit modes. You can put any content you want in a DataTemplate assigned to the CellTemplate (and optionally CellEditingTemplate). The template allows each individual row to create its own instances of the elements in the template.
I need to do this in code, not XAML.
Its not possible to programmatically create the content of a data template in the same way you might create Controls and add them to a UserControl.
Instead you will need to use System.Xml.Linq.XDocument to construct the DataTemplate as XML with DataTemplate being the root element. Once complete you can retrieve the XML string for the Root element and then use XamlReader.Load to get a constructed DataTemplate.
With a ListBox, you can specify a DataItemTemplate - just put whatever controls you want in that DataItemTemplate (e.g. you might want multiple TextBlocks), and bind them to whatever properties you want on the bound object.
To load the DataItemTemplate dynamically, you can use the same technique as illustrated in this blog post.
I have a user control whose users I want to have them set a DataContext on to bind to a list of objects. In my control, however, I want to display that list in a Grid, but in a non-trivial order. The column/row of display of each element will be determined by some code I will write.
So I cannot do a straight databinding in my control, I need to write code that will read the DataContext and then do the processing to correctly position each element.
How would a relative WPF newbie go about doing that? I guess the part I don't understand is what the code in my usercontrol will look like to read the DataContext items so that I can process them.
EDIT: Clarification: I want to stress I want to bind to the XAML Grid element, not some other kind of grid or DataGrid. Thx!
One of the possible way to achieve that is to use a Converter. You could create a Converter which converts the input list into another list where the order has been changed. Then you could use a "normal" databinding which will use the Converter.