WPF - Create ComBox with multiple separators - c#

I want to make a ComboBox with 0-N Separators
I was able to do this with CompositeCollection like so
<ComboBox.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection ={Binding mysrc1}/>
<ComboBoxItem IsEnabled = "False">
<Separator Background = "White" Height=10/>
</ComboBoxItem>
<CollectionContainer Collection ={Binding mysrc2}/>
<ComboBoxItem IsEnabled = "False">
<Separator Background = "White" Height=10/>
</ComboBoxItem>
</CompositeCollection>
</ComboBox.ItemsSource>
But this is kind of clunky and with a lot of code duplication but in .View and in .ViewModel
I was thinking about using custom control that will Inherit from ComboBox and use DependencyProperty.RegisterAttached for the location of the Separator(s)
but couldn't find a way to do so
P.S
i prefer not use any code behind and not use <Setter Property="Template"> because i have base style and template that a lot of ComboBoxs in my program user

As you are using databinding, the items are grouped, so I would using grouping.
See this answer: Grouping items in a ComboBox (solved)
If you do not want to use grouping, then you could use this method: Insert ComboBox Item Separator Which is Filled Through Data Binding

Related

Xceed CheckComboBox not properly showing selected values from items hardcoded in XAML

I try to use a CheckComboBox control from the Xceed library.
When I select an item, the control displays system.windows.controls.comboxitem:"value" instead of just value
Is there a way to display only the value of the selected item without its type?
<xctk:CheckComboBox>
<ComboBoxItem Content="SA" />
<ComboBoxItem Content="NA" />
</xctk:CheckComboBox>
In this particular case it can be solved by adding the DisplayMemberPath="Content"
<xctk:CheckComboBox DisplayMemberPath="Content">
<ComboBoxItem Content="SA"/>
<ComboBoxItem Content="NA"/>
</xctk:CheckComboBox>
But whether it is a designed feature or just a serendipitous behaviour, I am not sure.
So overall it would be better in the future to read on WPF binding, and use datasources and bindings to make the code similar to what is on the documentation page.
<xctk:CheckComboBox
DisplayMemberPath="Color"
ValueMemberPath="Level"
SelectedValue="{Binding SelectedValue}"
SelectedItems="{Binding SelectedItems}" />

ComboBox not displaying value in windows pone 8.0

I am trying to display values of all items present in a comboBox. The items are displaying as list after running the emulator.In addition, the combobox is not clickable to select an item.
<ComboBox FontWeight="Bold" x:Name="FStyle" HorizontalAlignment="Left" Margin="192,435,0,0" VerticalAlignment="Top" Height="27" Width="154" SelectionChanged="FStyle_SelectionChanged">
<ComboBoxItem Content="Normal"/>
<ComboBoxItem Content="Italic" />
</ComboBox>
What could be the problem.
Maybe it's stupid but ...
Remove that killing margin and set Height to auto, also as a general tip for future never hardcode Height and Width, but try to position controls in containers controls.
Good luck.

CompositeCollection doesn't propagate changes in underlying collections

I have what I thought was an ideal scenario for a CompositeCollection, except it seems that changes to the items of the underlying collection do not appear in the CompositeCollection (or, at any rate, not in the control whose source is this CompositeCollection).
EDIT 1: both underlying collections are ObservableCollections.
EDIT 2: the new/updated item gets added, but the contents of that item are not reflected in the drop-down area of the combobox. Each item implements INotifyPropertyChanged.
Am I doing something wrong or is this not supported?
Here's what I have:
<ComboBox SelectedItem="{Binding Products}">
<ComboBox.Resources>
<CollectionViewSource x:Key="CustomProductsSource" Source="{Binding CustomProducts}" />
</ComboBox.Resources>
<ComboBox.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={x:Static local:Products.Standard}}" />
<Separator/>
<CollectionContainer Collection="{Binding Source={StaticResource CustomProductsSource}}"/>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
What type of collections are Products.Standard and CustomProducts? If they do not have change notification (do not implement INotifyCollectionChanged), then CompositeCollection has no means to know that something changed.
ObservableCollection<> for example should work just fine in this scenario.
I fixed it - the problem was (as it is most often is) an idiot programmer (that would be me).
It so happened that Item of the collection (individual Product class) had an overriden ToString() method returned the Name property of the Product instance. So, the combo box was showing what it was supposed to show, which is the name of the product, except it was doing it based on the ToString() method... BECAUSE I FORGOT TO CREATE A DATATEMPLATE for the ComboBox. No wonder then that changes to the Name were not reflected

Remove elements from window based on Combobox selection xaml

I have the following situation: inside xaml I get values from a database and fill the combobox..if the selected item in the combobox has a value "x" I want to hide some elements from the working window..thx for your tips
<TextBlock Text="XYZ:"/>
<ComboBox ItemsSource="{Binding DataContext.KeyLists.XYZ,
RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
SelectedValuePath="XYZId"
SelectedValue="{Binding XYZId, Mode=TwoWay}"
DisplayMemberPath="Name" />
There are many ways to solve this.
you can make IValueConverter to convert selection values to Visibility, apply to each control with different converter parameter
you can write styles with triggers for the convtols
you could (not recommended) handle this in code

How to dynamically generate MenuItems using x:Reference?

I have a MenuItem whose ItemsSource is set to the following CompositeCollection:
<CompositeCollection>
<MenuItem x:Name="SpinnerMenuItem" Header="Waiting..."/>
<CollectionContainer
Collection="{Binding DataContext.Source,
Source={x:Reference SpinnerMenuItem},
Converter={StaticResource NoOpConverter}}"/>
</CompositeCollection>
The breakpoint inside my NoOpConverter is telling me that my collection is successfully getting bound to the CollectionContainer. The problem is, the menu is showing up completely empty! All I get is a popup about 3 pixels high and 10 pixels wide.
Why are my menu items not being displayed? Even the "SpinnerMenuItem" disappears once the bound list is populated. I was not having this issue in the simpler case, when I was just binding to a CollectionViewSource static resource.
This appears to be an issue with CompositeCollection. The workaround is to use a StaticResource instead of a Binding or similar. More info here: Why is CompositeCollection not Freezable?

Categories

Resources