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.
Related
I have a UserControl that has an ItemsControl. This ItemsControl is bound to a list of objects. To display those objects, I have *DataTemplate*s that determine how the object is presented.
I want to be able to retrieve the current values of the controls, such as Text if it is a TextBox, or SelectedItem if it is a ComboBox.
How do I iterate over these dynamically generated controls? I do not know the names of the controls, as they are generated during runtime.
Thanks..
The simplest why I could think of besides searching the visual tree for the controls and theirs template properties is probably to bind the ItemsControl data source to an ObservableCollection. This why you can dynamically add any type of controls to the list.
Once you do that, you can easily check the type of each UIElement within the list. e.g; if it's a Textbox then get the text property by casting, etc.
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.
I want to create my own expandable/collapsable tree-like UserControl, which nodes are the Border elements with any content. And this control should have the single SelectedItem. When I select one of the nodes I want to see details information about selected item.
I've done the control's presentation and a piece of logic:
I can see all the tree of elements, collaps any node(s), select one of them and see details. But I can't do bidirectional binding. I.e. I want to have an opportunity to change fields in details panel and immidiately see changes in the tree-control.
Help me please either call force update (rebind data) of the control (just give a tip how to perform this) or give an advise how to create my own List-like UserControl.
I tried to make my control inherit from some ListBox class but I couldn't.
PS. Sorry for my bad English...
Use the TreeView control.
I'm looking to create a WPF textbox control that acts similar to the email recipient textboxes in Outlook (the To, Cc and Bcc inputs). I don't necessarily care that much about auto-completion (i've found millions of examples for that), but what I'm really struggling with is how to have delimited text entries in the textbox behave as entities the way they do in Outlook (once a recipient you've entered resolves, that text becomes an 'entity' which you can click to select, right click to get a context menu, etc. it's not longer 'plain text' in which you can place your cursor)...
Does anyone have any high level ideas how to accomplish this? Know of any existing examples (I've googled for hours)?
Thanks so much in advance,
Michael.
My rough thought process would be this... (note: I'm not actually coding it, so my details may be a little off...).
High level behaviour:
the type of data in your control is a list of items which aren't selectable. Therefore your control is, approximately, an ItemsControl (in terms of visual/XAML, it's an ItemsControl with a WrapPanel style layout and very simple TextBlock for the item template).
when your control gains focus, you need to switch the template to be a TextBox
when your control loses focus, you need to split the inputted text and convert it to a list for display.
Therefore, thinking code:
you need a UserControl, possibly derived from ItemsControl. That gives you basic behaviour to represent a list of items.
you need a custom DependencyProperty on your control that represents the delimited string.
when the string property changes, you need to parse it and replace the list of items in the control.
when the list property changes, you need to replace the string property with a suitably-delimited list.
In terms of code-behind, that part should be pretty simple. Then, for the XAML template...
you need a base template that displays your Items property as a list, using the WrapPanel layout mentioned above.
you need a trigger that replaces this template when the control has focus.
the replacement template should be a TextBox that is bound to the string property of the control.
the default binding behaviour on a TextBox will only push a new value when the TextBox loses focus, so you need to think about whether you want to make, say, an "Enter" keypress move focus (thus reverting the template to the list version - when the string property's value changes, your codebehind will update the list).
This should give you the basic behaviour. You should be able to bind either the list property or the string property from outside of the control, though you may have to be careful about what happens if you bind both properties since there's a two-way dependency between them...
I've got a Combo Box that is databound to an ObservableCollection of items. I would like to have a default selected item that is (None) that would set the value of the property I've bound to "SelectedValue" to null.
I think there ought to be a way to achieve this with some combination of Style/DataTemplate/TemplateSelector. I'm trying to design this with MVVM in mind, so I'd like something that doesn't use codebehind and is as reusable as possible. I'd also like the benefits of the ObservableCollection (updating the collection causing the control to rebind) to remain intact.
Bonus part B:
I would like to also be able to append an extra visual element to the bottom of an ItemsControl as well. I was thinking it would be easy to change the DataTemplate if I knew how to trigger it on the last item of a collection. Willing to entertain other options here.
The simplest way I've found to do this is to insert a "special" value into the underlying collection, and display the "(None)" text when it's selected. Obviously then you need to run your binding through a converter to take this value into account and return null when it's selected. (See this question of mine which was a result of me trying to add an actual null value to a ComboBox's underlying collection.)
Having said that, it might actually be possible to do what you want with the CompositeCollection class. You could make a separate collection (with only one item - your Null item) and bind your ComboBox to both it and your original collection through the CompositeCollection.