Combobox CompositeCollection - wpf

I want two comboboxes with collection with empty element.
I Use two comboboxes with:
<ComboBox x:Name="itemEditPageComboBox"
...
ItemsSource="{StaticResource ItemsColl}"
....
/>
Collections:
<CompositeCollection x:Key="ItemsColl">
<ComboBoxItem Content="" />
<CollectionContainer Collection="{Binding Source={StaticResource ElementsCollection}}" />
</CompositeCollection>
<CollectionViewSource x:Key="ElementsCollection" Source="{Binding Path=...}" />
behaviour
Sequentially choose two comboboxes, after it - one of them is removed empty element.
Error:
System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='ComboBoxItem'
Please tell me what to do?
Best regards!

You cannot use the same ComboBoxItem in two ComboBoxes, also the error should be quite clear: If you add a ComboBoxItem to a ComboBox which has an ItemTemplate defined, that won't be applied to that ComboBoxItem because it already has the type of the created containers. This error may or may not be a problem, depending on what you want.
If you want to use the collection for two ComboBoxes you should add a string or if you want something more complex an instance of a class (which should probably have an implicit DataTemplate associated with it). Do not add a ComboBoxItem directly.

Related

Why ItemTemplate will make items disappear for ListView?

I was solving Windows Phone 8.1 ListView wobbling problem and had code like below, however, once I add the ItemTemplate, the contents of the List cannot be seen, I'm wondering why and how to fix the problem.
<ListView
Grid.Row="1"
x:Name="ListViewEvents"
Loaded="OnListViewEventsLoaded"
ItemsSource="{Binding xx}"
ItemTemplateSelector="{StaticResource xx}"
ItemContainerStyle="{StaticResource xx}"
IsItemClickEnabled="True">
<ListView.ItemTemplate >
<DataTemplate >
<Grid Width="{Binding ActualWidth, ElementName=EventsListGrid}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Your Width binding is trying to read the ActualWidth property of an element in your XAML named EventsListGrid, but there's no such element in the sample code you've provided. As such, the binding engine is unable to set the Width property on your grid, most likely setting it to some unset/NaN value. At least I can confirm this when setting up a similar case as the one you provided and inspecting in Snoop the ListViewItem containers generated for each item in the test collection bound to the ListView. Perhaps you want to set the element name to ListViewEvents in this case or some other parent element not shown in the example?

wpf debug error output System.WIndows.Data Error 25

I have a custom styled Combobox which works fine. It is placed inside a usercontrol and bound to a data structure. I use DisplayMemberPath to show only one element in the Combobox TextBox. The ComboBox Style is taken from MSDN and used many times. So it's not displayed here.
<UserControl x:Class="wpf.projext1.MyComboBox"
x:Name="MyControl"
...
<ComboBox Style="{StaticResource ComboBoxStyle}"
Text="{Binding ElementName=MyControl, Path=Text}"
IsEditable="True"
IsTextSearchEnabled="False"
StaysOpenOnEdit="True"
ItemsSource="{Binding ElementName=MyControl, Path=MyItemsSource}"
DisplayMemberPath="Name"
</ComboBox
I get the following annoying error message populating the output window:
System.Windows.Data Error: 25 : Both 'ContentTemplate' and 'ContentTemplateSelector' are set; 'ContentTemplateSelector' will be ignored. ComboBoxItem:'ComboBoxItem' (Name='')
if i leave out the
DisplayMemberPath="Name"
... no debug output about error 25 is shown. But I definitely need DiplayMemberPath="Name"!
Do You have an idea to fix this ?
You canĀ“t set both DisplayMemberPath and ItemTemplate at the same time.
DisplayMemberPath is used to tell the ItemsControl which property to display when showing your objects. It makes no sense to set this field if you're already passing a custom ItemTemplate, since you can choose how to show the object within that ItemTemplate.
Since the default Combobox style from MSDN also sets an ItemTemplate, this is likely the cause of the error.
resolved: use the TextSearch attached property, no matter if TextSearch is enabled!
TextSearch.TextPath="Name"
Should note TextSearch.TextPath="Name" instead of DisplayMemberPath="Name".

Bind RibbonComboBox.SelectionBoxItem

I'm trying to bind the item selected in a RibbonComboBox to a property of an object. The problem I'm encountering is that the RibbonComboBox.SelectionBoxItem only provides a get accessor; therefore, I cannot bind it to anything in the XAML.
Any ideas how to bind the item to the property of an object? I could use a regular ComboBox is there another more appropriate control?
xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
<ribbon:RibbonComboBox
ItemsSource="{Binding Source={StaticResource CollectionOfPossibleChoices}}"/
SelectionBoxItem="{Binding Path=PropertyToBindTo}"/> <!--Not valid-->
RibbonComboBox is unlike ComboBox (which i, also, find confusing). Try this;
<ribbon:RibbonComboBox>
<ribbon:RibbonGallery SelectedItem="{Binding Path=PropertyToBindTo}">
<ribbon:RibbonGalleryCategory ItemsSource="{Binding Source={StaticResource CollectionOfPossibleChoices}}" />
</ribbon:RibbonGallery>
</ribbon:RibbonComboBox>
MSDN Reference

How to insert item to combobox after binding wpf

I want to add an item to the combobox after binding it.
for example:
this.cbCategory.ItemsSource = categoryList;
this.cbCategory.DisplayMemberPath = "CategoryName";
this.cbCategory.SelectedValuePath = "CategoryID";
i want to add("All", "%") as the first one.
Geetha.
This is very simple using a CompositeCollection:
<ComboBox DisplayMemberPath="CategoryName" SelectedValuePath="CategoryID">
<ComboBox.ItemsSource>
<CompositeCollection>
<my:Item CategoryName="All" CategoryID="%" />
<CollectionContainer Collection="{Binding CategoryList}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
How it works: The CompositeCollection produes the "All" item followed by all of the items in the CategoryList collection. Note that <my:Item ... /> is the constructor for your item class. You will need to change it to your actual namespace and class name.
Important advice: I notice you are setting some ComboBox properties in code-behind. This is a very bad practice. You should use XAML as shown above.
You'll break the binding if you try to add it later and will no longer receive updates.
Why not just add your 'extra' item before you bind it?

WPF StaticResource bound to subproperty

I have a class thats created as a resource:
<Window.Resources>
<Model:MyModel x:Key="model" />
</Window.Resources>
The MyModel class has a cli property named Foo. I want the selected value in a combobox to be bound to this property. I thought I could do this but Im getting errors:
<ComboBox SelectedItem="{Binding Source={StaticResource model.Foo}}" />
Heres the error:
Cannot find resource named '{model.Foo}'.
Where did I go wrong? What extra parameters do I need to specify to properly bind to a subproperty?
You almost have it correct. You want to use a combination of the Binding's Path property and its Source property. So use one of the following (they are equivalent.)
{Binding Foo, Source={StaticResource model}}
or
{Binding Path=Foo, Source={StaticResource model}}
Hope this helps.

Resources