How to make combobox searchable by keywords - c#

Althrough i try searching,i can't find any.I'm using visual studio 2019.
Here's my combobox.
<ComboBox
Name="combo"
IsEditable="True"
SelectedValuePath="Id"
DisplayMemberPath="Name"
ItemsSource="{Binding Data.CodeList, Mode=OneWay, Source={StaticResource Proxy}}"
SelectedItem="{Binding Path=SelectedItem}"/>

Have you tried with IsTextSearchEnabled property
Gets or sets a value that indicates whether TextSearch is enabled on the ItemsControl instance.
You can also check Combobox IsTextSearchEnabled = True validating entered text to check if it is present in Itemsource for checking against speciific values and rollback when they don't exist.
If you want to go before windows 10, then you'll have to use a custom control (effectivly create your own combobox.

Related

WPF ComboBox bind text to selected item

I try to bind a ComboBox to a collection:
<ComboBox Margin="4 0 2 0"
ItemsSource="{Binding YAxes}"
SelectedItem="{Binding SelectedYAxis, Mode=TwoWay}"
DisplayMemberPath="AxisTitle"
SelectedValuePath="AxisTitle"/>
Everything is fine, except Text of this ComboBox. On selection of item, the setter on SelectedYAxis fires and notifies, that property has been changed:
private IAxis _selectedYAxis;
public IAxis SelectedYAxis
{
get => _selectedYAxis;
set
{
_selectedYAxis = value;
OnPropertyChanged(nameof(SelectedYAxis));
}
}
but the text on ComboBox never changes to the selected items AxisTitle. How to display an AxisTitle of SelectedItem as a text of ComboBox?
UPD: Text is never shown, even if it's set explicitly:
<ComboBox Margin="4 0 2 0"
ItemsSource="{Binding XAxes}"
SelectedItem="{Binding SelectedXAxis, Mode=TwoWay}"
DisplayMemberPath="AxisTitle"
Text="Asdasd"/>
It doesn't set the text of ComboBox to "Asdasd".
UPD 2: I've changed the things to use DataTemplate, but this didn't work as well:
<ComboBox Margin="4 0 2 0"
ItemsSource="{Binding YAxes}"
SelectedItem="{Binding SelectedYAxis, Mode=TwoWay}"
ItemTemplate="{StaticResource AxisCBTextTemplate}"/>
And the resource section above:
<DataTemplate x:Key="AxisCBTextTemplate">
<TextBlock Text="{Binding AxisTitle}"/>
</DataTemplate>
UPD 3
An illustration to what do I mean:
The task of displaying some selected text should be trivial, but it has difficulties.
I've found the root cause of this issue. Each IAxis (it is a SciChart axis object) from XAxes and YAxes is already dispayed on the graph (i.e. bound). Binding them to other controls (like ListBox) causes an exception: "Must disconnect specified child from current parent Visual before attaching to new parent Visual.", I found it out while trying to bind them to ListBox.
Seemes like ComboBox catches such exceptions and doesn't output StackTrace for any case. In my case this exception was wrapped into NullReferenceException and occurred only on click on a ComboBox, that has no ItemTemplate set. Though I may not be fully correct in details, replacing XAxes and YAxes with collections of strings solves this issue.

Set the SelectedText of a ComboBox in WPF/MVVM

Heyo its me again,
<ComboBox ItemsSource="{Binding EnterpriseList, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedItem="{Binding Enterprise, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
DisplayMemberPath="Name"
IsEditable="True"
IsEnabled="{Binding EnterpriseIsEnabled, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Margin="3" Grid.Column="1">
does anyone know how i can bind the SelectedItem in this example to an Enterprise-Object?
I can select the enterprise in the combobox like I want it, and it delivers a enterprise-object too, but when i reload the view, the text of the combobox doesnt get set to the name of the enterprise-object.
I hope I have explained my problem well enough, my english is not that good, I usually speak german ...
Your XAML look ok. Please be aware that the item your assign to Enterprise must be one from the list EnterpriseList. The Combobox does compare object references not object contents e.g. the characters of strings.

Unable to bind Contact object to controls without editing the collection

I have a collection of Contact objects that I've bound as follows in a WPF form:
<ComboBox Name="Name"
Text="{Binding Path=Contact.FullName}"
ItemsSource="{Binding ContactsCollection}"
SelectedItem="{Binding Path=Contact, Mode=TwoWay}"
IsEditable="true"
IsTextSearchEnabled="True"
TextBoxBase.TextChanged="Name_TextChanged"/>
<TextBox Name="Position" Text="{Binding Path=Contact.Position}"/>
<TextBox Name="Phone" Text="{Binding Path=Contact.PhoneNumber}"/>
I'd like the contact to be selected when the user starts typing in the combo 'IsTextSearchEnabled=true'.
The problem is that I'd like the items in the collection to remain read-only. Once a contact has been selected, any text deletes or additions modify the contact name in the collection.
How can I bind a collection to a combobox, enable search and prevent edits to the collection?
I could be missing something here, but if you don't want an editable ComboBox, try not setting the ComboBox.IsEditable property to True. Using this simple code, I can display items in a ComboBox and make selections by typing (when the ComboBox is focused) without editing anything:
<ComboBox ItemsSource="{Binding Items}" IsTextSearchEnabled="True" Height="25" />
Thanks for your input. It sorted me out. Removing the SelectedItem property and setting Position and PhoneNumber in the PropertyChanged event was what I needed.

RibbonComboBox doesn't display selected item

Very simple program. Maybe too simple?
XMAL:
<RibbonComboBox x:Name="cbxRibbonCommsGroupBaud" LargeImageSource="Resource/Cheetah.png">
<RibbonGallery Name="RBaudGGallery" SelectionChanged="RBaudGGallery_OnSelectionChanged">
<RibbonGalleryCategory Name="RBaudGGalleryC" ItemsSource="{Binding}"></RibbonGalleryCategory>
</RibbonGallery>
</RibbonComboBox>
The code behind:
private int[] baudRateList = { 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600 };
cbxRibbonCommsGroupBaud.Items.Clear();
cbxRibbonCommsGroupBaud.ItemsSource = baudRateList;
When I run the program, the items are in the combobox drop down but when I select the item, it doesn't stay and the box appears empty. Also, RBaudGGallery_OnSelectionChanged is never called. So I'm missing something but have no clue what since other combo boxes are working just fine that aren't in the ribbon. I'm using the Reference of System.Windows.Controls.Ribbon.
Why don't you have a SelectedItem binding set?
SelectedItem = {Binding mySelectedBaud}
How to databind SelectedItem of RibbonComboBox
I think I might have found another answer : IsSynchronizedWithCurrentItem="True" on the RibbonGallery control sets the SelectedItem correctly
<RibbonGallery
IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding SelectedRule, Mode=TwoWay}"
x:Name="RulesItems" >
<RibbonGalleryCategory
ItemsSource="{Binding RulesCollection, Mode=TwoWay}"
DisplayMemberPath="DisplayName" />
</RibbonGallery>

ComboBox to select first Value when created

I have a combo box in a ItemControl. xaml is
<ComboBox ItemsSource="{Binding DataContext.NodeMembershipFunction,
RelativeSource={RelativeSource AncestorType={x:Type ItemsControl},
AncestorLevel=1}}"
DisplayMemberPath="_Name"
SelectedValue="{Binding Condition, Mode=TwoWay}"
SelectedValuePath="_Type">
</ComboBox>
My combobox works fine with above so I am not posting any code to explain above.
My problem is that when I add a new item to my ItemControl, the combox has nothing selected (which is correct according to the code I have). Is there any way to add a trigger or something in above which selects first item only when nothing is selected eg on adding new itemcontrol?
Set IsSynchronizedWithCurrentItem="True" on comboBox instance so that it always be in sync with current item of collection.
<ComboBox IsSynchronizedWithCurrentItem="True"..../>
Moreover, adding item in collection won't make SelectedItem to go away unless you are re-initializing the entire list.
I would suggest to use ObservableCollection<T> for property NodeMembershipFunction in case not doing it already and add item directly to the collection instead of repopulating it.

Categories