Set the SelectedText of a ComboBox in WPF/MVVM - c#

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.

Related

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.

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.

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

How to Bind with Combobox in silverlight

I want to bind my collection of countries to a Combobox.
My XAML page looks as follows:
<pmControls:pmComboBox
Margin="3" Grid.Row="5" Grid.Column="1"
ItemsSource="{Binding Path=Countries}"
SelectedValue="{Binding Title,Mode=TwoWay}" >
</pmControls:pmComboBox>
I want to display Title as DataTextField.
Currently it shows namespace of country class in my combobox list .
I have also tried to add DisplayMemberPath but it also not works.
How can I setup the display field of Combobox to use a Binding?
I think you need to do
DisplayMemberPath="Title"
SelectedValuePath="Title"
SelectedValue="{Binding Path=Country,Mode=TwoWay}"

C# Silverlight - Combobox with TwoWay Binding?

EDIT:
The Answer I was looking for was....
<dataFormToolkit:DataField Label="Business Type:">
<ComboBox x:Name="BusinessType" SelectedItem="{Binding BusinessType, Mode=TwoWay}" >
<ComboBox.Items>
<sys:String>Land</sys:String>
<sys:String>Maritime</sys:String>
</ComboBox.Items>
</ComboBox>
</dataFormToolkit:DataField>
Heres the link to the article
I have a c# silverlight business application that uses the ado.net entity framework and the domain service class to bind to my sql server database and pull data from/ persist data to my database. I have been using the dataformtoolkit namespace to layout textboxes which can be edited/display data, using a TwoWay binding mode in order to allow the read/write functionality.
On some of the fields I want to use a combobox instead of a textbox in order to add a better user experience to my application. The impression i've got from reading around the web is that this isnt as easy as it should be.
My current textbox code looks like:
<dataFormToolkit:DataField>
<TextBox Text="{Binding BusinessType, Mode=TwoWay}" />
</dataFormToolkit:DataField>
my attempt at something similar is as follows but incorrect:
<ComboBox>
<ComboBox.Items>
<ComboBoxItem Content="Maritime" IsSelected="{Binding BusinessType, Mode=TwoWay}" />
<ComboBoxItem Content="Land" IsSelected="{Binding BusinessType, Mode=TwoWay}" />
</ComboBox.Items>
</ComboBox>
NB/ I want the combobox to be populated by a list or an enum etc. (preferably a list). The contents of the combobox should have nothing to do with the database, just that when the user hits submit the selection made in the combobox gets persisted back to the database. It is also important that the combobox can read from the database and display the specific selection that has already been made if this is the case.
****EDIT:
Current setup of dataform bound to datagrid with editable businesstype field as a textbox (I want to replace this textbox with a combobox that has two selectable items).
<!--DataForm Declaration-->
<dataFormToolkit:DataForm x:Name="dataForm1" Height="410" Width="331"
VerticalAlignment="Top"
Header="Job Details"
CurrentItem="{Binding SelectedItem, ElementName=dataGrid1}"
HorizontalAlignment="Left" >
<dataFormToolkit:DataForm.EditTemplate>
<DataTemplate>
<StackPanel>
<dataFormToolkit:DataField>
<TextBox Text="{Binding BusinessType, Mode=TwoWay}" />
</dataFormToolkit:DataField>
</StackPanel>
</DataTemplate>
</dataFormToolkit:DataForm.EditTemplate>
</dataFormToolkit:DataForm>
So how do i manipulate this code to use a combobox instead of a textbox?
Any help in doing this would be greatly appreciated.
You should setup your binding to use the ComboBox's SelectedValue property.
<ComboBox SelectedValue="{Binding BusinessType, Mode=TwoWay}">...</ComboBox>
The problem with this is that the ListBox and ComboBox will use the Equals() method on the object in the SelectedItem so if the types do not match then the ComboBox will not set the appropriate item as selected. Therefore, BusinessType will need to be a string since you are using ComboBoxItem's and specifying string content.
If bound the ItemsSource of the ComboBox then you would use SelectedItem and it would actually be an entity type as well, in which case you have more flexability/control around what equals what.

Categories