WPF MVVM StackPanel dynamic children type - wpf

I have a list of properties that have a value and a type, and depending on the type, various controls should be added to stackpanel. ie - for text TextBox should be used, for double - some sort of numericupdown control. Can this be implemented with MVVM? Any help is appreciated.

You can use an ItemsControl with implicitly applied DataTemplates (add them to Resources, set DataType only (use x:Type)).
ItemsControl because the StackPanel is the last step that only allows UI elements as children, ItemsControl accepts data as items.

Related

Use parts of default WPF controls for binding

Is it possible to use the parts of default WPF controls for binding?
Specifically, I'm aiming at binding the padding size of a ListBox control to the width of the arrow part of a ComboBox control.
Using VS 2015 Live Visual Tree, I can see that the specific part I'm interested in is named 'splitBorder', which is the portion of the ComboBoxToggleButton that contains the downwards arrow symbol.
Specifically, I'm aiming at binding the padding size of a ListBox control to the width of the arrow part of a ComboBox control.
No, you can't really do this since the width of the arrow and the arrow itself is hardcoded within the ControlTemplate of a ToggleButton that is part of the default ControlTemplate of the ComboBox.
You cannot bind to the arrow Path from any element outside of the control template where it is defined and the ComboBox class doesn't expose the arrow from any public property that the ListBox can bind to. So this is not possible I am afraid.

WPF Toolkit autocompletebox itemtemplate

I'm following this reference to implement an autocomplete textbox in my application:
http://www.broculos.net/2014/04/wpf-autocompletebox-autocomplete-text.html
I notice there is a difference when using an itemtemplate vs not using one. When using a template the items have more space between them. I need to use the itemtemplate but how can I make it look more like when not using a template, move the items closer to each other?
When not using an ItemTemplate, the single items in the dropdown list will be simple strings and will be represented by TextBlocks in the UI. TextBlocks don't have any margin by default, so the lines are close together.
Assigning an ItemTemplate will replace this visual appearance (with whatever the ItemTemplate defines). Most probably there is some margin/padding defined in the ItemTemplate, e.g. if an Label is used as representation (Labels have some default margin assigned).
Changing the margin/padding properties of the single elements of your ItemTemplate should help here.

Iterating over dynamically generated controls and getting certain properties

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.

Display a variable-sized collection of TextBlocks/ComboBoxes in Silverlight 3

I have a list of textual descriptions, each of which a user must score on a scale of 1-5. The number of textual descriptions is variable, so I can't just define a static Grid in XAML.
The following image shows approximately what I'm after:
In ASP.NET, I could bind my list of text items to a Repeater control. For each row, the text would be displayed in a Label, and next to it would be a DropDownList that contains a static list of items. Is there a similar control available in Silverlight?
Should I be using one of these?
DataGrid
ListBox
Custom control derived from ItemsControl
(Other)
I solved this by using an ItemsControl, and within the ItemTemplate I placed the TextBlock and ComboBox controls.
The only wrinkle for me was that the ComboBox depended on a separate DataContext, so I had to resort to using the DataContextProxy described in this article

Expand WPF Expander to bring contained element into view

I have an expander that has n contained elements (possibly other Expanders that also contain elements).
Now I want to programmatically bring a contained element into view - like with BringIntoView() for ScrollViewers. All Expanders that currently hide the element should expand.
My current idea is to subclass the Expander and make it react to an event that bubbles up from the contained element. But there may be a much easier way in WPF, right?
You can create an attached property to do that instead of subclassing the existing Expander class. This AP would be of type bool, and when sets to True on an expander register for the Expanded event. In the event handler, you can walk up the logical tree to grab the parent Expander and toogle is IsExpanded property.

Resources