Sort combobox alphabetically WPF [duplicate] - wpf

This question already has an answer here:
Sorting a combobox purely in XAML
(1 answer)
Closed 1 year ago.
I have a lot off combo boxes in my project. I need to sort there items alphabetically.
I can't sort them individually in SQL query not through LINQ where data in loading .
I want to put a hook in Combox ,whenever any combox get data in project it sort in automatically.
How I can do that ? What and where changes should I made to generically sort all combobox.

You can use CollectionViewSource
On XAML side:
Here you need to set cm as namespace
xmlns:cm="clr-namespace:System.ComponentModel;assembly=System"
...
<UserControl.Resources>
<CollectionViewSource x:Key="MyViewSource" Source="{Binding Path=YourItemSource}">
<CollectionViewSource.SortDescriptions>
<cm:SortDescription PropertyName="Name" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</UserControl.Resources>
... Usage ..
<ComboBox ItemsSource="{Binding Source={StaticResource MyViewSource}}" />

Related

Can I setup a CollectionViewSource for a XAML non-bound ListBox sort?

I referenced this SO post but don't understand how the < CollectionViewSource > is supposed to be setup. I am working with this very simple ListBox:
<Window xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" ...
<ListBox>
<CollectionViewSource>
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Content" Direction="Ascending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
<ListBoxItem>ctext1</ListBoxItem>
<ListBoxItem>btext2</ListBoxItem>
<ListBoxItem>atext3</ListBoxItem>
<ListBoxItem>dtext4</ListBoxItem>
</ListBox>
This treats the < CollectionViewSource > element as another item in the ListBox collection. I assume this is because < CollectionViewSource > is not setup properly, but since I'm just learning this, perhaps I have a basic misunderstanding. I understand the sort would normally be done in code behind along with a view model, but I'm just trying to understand how the sort is accessed in pure XAML.
1) Is it possible to setup the sort for this simple ListBox implementation?
2) What is required?
You need to use the CVS as resource, and reference to it.
https://wpftutorial.net/DataViews.html
Look under: How to create a CollectionView in XAML

How to bind to multiple sources without codebehind or using SelectedValue

While trying to convert usual wpf fields to the custom fields that the program I need to modify, I came across an issue of having 2 different data sources.
1) The data source that retrieves/inserts data to fill this combobox (DataSource)
2) The data source that takes care of other UI elements (DSP)
As when certain items are selected from the combobox, not only does it get stored with the other information in the form, but it may show/hide another UI element.
I am trying to convert:
<ComboBox Name="tempComboBox"
ItemsSource="{Binding Source={StaticResource DataSource}, Path=Value.Properties[temp].MetaData.Lookups}"
DisplayMemberPath="Description"
SelectedValuePath="Value"
SelectedValue="{Binding Source={StaticResource DSP}, Path=Value, ValidatesOnDataErrors=True}"
Style="{StaticResource tempComboStyle}"/>
Into something like this:
<ctrls:Fields Name="tempComboBox"
FieldName="temp"
DataContext="{Binding Source={StaticResource DataSource}, Path=Value, ValidatesOnDataErrors=True}"
Style="{StaticResource tempComboStyle}"/>
However, this WILL NOT work as it only stores the data, but does not hide/show elements when the specific item is selected.
I have tried multi binding, which does not work. Surrounding the combobox tags with the ctrls:Fields tag, again doesn't work. And combining the DataContext property with both SelectedValue and ItemSource, none of which work.
I do not have any way of getting to the code behind of this form either, it must be strictly done through XAML.
Thank you for any help!

bind data to combobox in datagrid in silverlight

how can i bind the data returning from WCF in combox in grid . the combobox is not in edit mode. I am able to display the static data but not the data returning from WCF.
u can use RelativeSource to search needed DataContext/ViewModel/Page/UserControl in the elements tree
example for the ViewModel where DataGrid itemssource defined near ComBoxCollectionSource:
<ComboBox ItemsSource={Binding RelativeSource={RelativeSource AncestorType=sdk:DataGrid}, Path=DataContext.ComboBoxColloctionSource} />
I'm assuming the static data is the stuff you enter into the Items property by hand. The syntax for a combobox is a bit different, but here it is:
<ComboBox ItemsSource="{Binding Path=<your collection>,Mode=OneTime}" SelectedValuePath="<id-field>" DisplayMemberPath="<display-field>" SelectedItem="{Binding Path=<your-property>,Mode=TwoWay}" />
The parameters ending in PATH above just have the names of the properties as a string.
EDIT: If you're using a dictionary, you would use:
<ComboBox ItemsSource="{Binding Path=<your dictionsry>,Mode=OneTime}" SelectedValuePath="Key" DisplayMemberPath="Value" SelectedItem="{Binding Path=<your-int-property>,Mode=TwoWay}" />

WPF binding to individual 'rows' (not columns) of a datagrid possible?

I have a datagrid. A column of the datagrid is a simple <DataGridTemplateColumn> with its CellTemplate containing a <DataTemplate> which contains a <ComboBox> such as
<my:DataGrid Name="dataGridMain" AutoGenerateColumns="False">
<my:DataGrid.Columns>
<my:DataGridTemplateColumn Header="Food" >
<my:DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<ComboBox Name="comboDataTemplate"
Text="{Binding Path=Food,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Source={StaticResource resFoodLookups}}"
DisplayMemberPath="FoodName"
SelectedValuePath="FoodID" IsEditable="True" />
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
</my:DataGrid.Columns>
</my:DataGrid>
All is working fine. Each combobox is bound to a static list due to the ItemsSource="{Binding Source={StaticResource resFoodLookups}}" statement.
But my requirement is that this list will change from row-to-row.
That is: each time a user types a new entry in the combobox list on one row, I want to have it available in the selection on the next row.
Basically, I want to create a new list for the user each time the user inserts a new word in the combobox on any of the rows. (The combobox is editable).
Now, I can wire up the "ItemsSource=..." at run-time, but I'm only able to do this once thus the <DataTemplate> propagates the 'same' list to 'all' the comboboxes on 'all' the rows.
My thoughts are that I need to change the ItemsSource=... property on an object-by-object basis on each combobox that is created in memory after the DataTemplate has created them - but I have no idea how to do this.
What you need to do is perform 2 way data binding to your the ItemsSource, this way when the ItemSource is updated in one of the combo boxes it will auto update your original collection and therefore your other combo boxes as well.
What I normally do is use the MVVM pattern. It is worth some research if you are not already using a particular pattern on your application.
Using it to solve your problem i would do the following:
Create a ViewModel (Lets call it MyViewModel) which has a collection of values called 'MyComboBoxItems' (It is important that you use ObservableCollection for the databinding to work)
When I create the Window/Control that contains your table, I also create an instance of MyViewModel and set its the Window.DataContext=myViewModelInstance
For your combobox binding use ItemsSource="{Binding Path=MyComboBoxItems, Mode=TwoWay}

Tricky WPF styling issue - can you do it?

I have an observable collection of items, where each item has a name and a "group name" (both strings).
The tricky part is, in XAML, I need to style this collection such that all items with the same group name are listed next to each other, and the group name is shown at the top.
I have designed user controls for the layout of the group name, as well as the layout of each item being displayed. I have played around with ItemsControl but have no idea how to style it so that items with the same group are somehow "merged" into the same ItemsControl that uses the generic ItemControl user control as its source, with the group name displayed on top.
I did have this working by using a collection of "groups", with a collection of items inside each group, because the bindings work perfectly. However, I did not take into account that each item may be in more than one group (think of a person as being in a "work friends" as well as "colleagues" group, so it needs to be duplicated).
Any advice or solutions are much appreciated :)
First, use a CollectionViewSource to sort the items:
<CollectionViewSource x:Key="SortedItems" Source="{Binding YourUnsortedItems}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="GroupName"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
Then bind to it from your ItemsControl (or whatever):
<ItemsControl Source="{Binding Source={StaticResource SortedItems}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- stick your template for each item here -->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

Resources