RibbonComboBox doesn't display selected item - c#

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>

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.

How to make combobox searchable by keywords

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.

Trying to add a combobox within datagrid which has an itemsource already

If I have a combo box by iteself with an item source e.g.:
<ComboBox ItemsSource="{Binding Combobox.Options}"/>
This works fine. Where Combobox.Options is a list of strings {"option1","option 2",... etc}
However, I want to have a combobox within a datagrid which has an item source:
e.g.:
<DataGrid CanUserAddRows="False" AutoGenerateColumns="False" SelectionMode="Single" ItemsSource="{Binding Users.Table.Tables[0]}">
But I keep getting an empty combo box when I add it into a datagrid template column. I have also tried the Find Ancestor Relative soure but also got an empty combo box.
Finally managed to find the problem. Needed to add a DataContext when getting the list for the item source within the datagrid:
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}"/>

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.

WPF Combobox binding with List<string>

I have two properties, one which is a list of string and the other just a string.
private List<String> _property;
public List<String> Property
get
{
return new List<string>(){"string1", "string2"};
}
set{_property = value
}
public String SimpleStringProperty{get;set;}
I also have a Combobox defined in XAML as such
<Combobox ItemsSource="{Binding Property , Mode="TwoWay"}" Text="Select Option" />
Now the combobox correctly displays two options :"string1" and "string2"
When the user selects one or the other, I want to set SimpleStringProperty with that value. However, the 'value' im getting back from the combobox through the two way binding is not the selectedItem, but the List<String>. How can I do this right? I'm fairly new to wpf, so please excuse the amateurism.
<Combobox ItemsSource="{Binding Property}" SelectedItem="{Binding SimpleStringProperty, Mode=TwoWay}" Text="Select Option" />
That's untested, but it should at least be pretty close to what you need.
You need to bind to the String property using the SelectedItem property of the combobox.
<Combobox ItemsSource="{Binding Property}"
SelectedItem="{Binding SimpleStringProperty}"
IsSynchronizedWithCurrentItem="True"
Text="Select Option" />
What helped me:
Using SelectedItem
Adding UpdateSourceTrigger=PropertyChanged
IsSynchronizedWithCurrentItem="True" to be sure Selected item always synchronized with actual value
Mode=TwoWay if you need to update as from source as from GUI
So at the end best way, if source is
List<string>
Example:
<ComboBox
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding SomeBindingPropertyList}"
SelectedItem="{Binding SomeBindingPropertySelectedCurrently,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
Additional Info
Difference between SelectedValue and SelectedItem:
https://stackoverflow.com/a/4902454/2758833
https://stackoverflow.com/a/2883923/2758833
SelectedValuePath Documentation:
https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.primitives.selector.selectedvaluepath
SelectedValue updates possible bugs for .NET 4 and .NET 4.5:
https://stackoverflow.com/a/247482/2758833

Categories