kendo combobox selectedindex is not working - combobox

I've created this combobox:
#(Html.Kendo().ComboBox()
.HtmlAttributes(new { style = "width:180px" })
.Name("CompanyId")
.DataTextField("CompanyName")
.DataValueField("CompanyId")
.Text("")
.SelectedIndex(0)
.Suggest(true)
.Events(events => events.Change("OnCompanyChange"))
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetCompanysByCompanyID", "Company");
});
}))
Even though I've set the SelectedIndex to 1, it's always -1.
For Comboboxfor selectedindex is working fine but for combobox selectedindex is not working .It is not selecting first value by default even if I kept selectedindex to 0 or 1 or 2

I'm not sure, but I think SelectedIndex probably will not work if you are using a remote datasource.
Try to set your combobox's initial value after the datasource has finished requesting the data (through Datasource.read) at Events.DataBound.

I believe the reason you are having trouble is you set the .Text property to string.Empty. This would be overriding one or the other and in your case I believe it's replacing the value from the SelectedIndex.

Related

ListBox.SelectedItems is set when assigning ItemsSource in code-behind using async-await

I have a ListBox and I assign its ItemsSource manually in the code behind. This works fine at first, but when I try to do it asynchronously it starts acting weird. Right after I assign the ItemsSource the SelectedItems is getting set to the first element in the list. I can clear the item selections but I really want to know what's causing this.
lbResults.ItemsSource = await Task.Factory.StartNew(() =>
{
return Context.ResultsList;
});
The issue is most probably caused by ListBox.IsSynchronizedWithCurrentItem. Change the setting to false and your issue will be resolved.
XAML :
IsSynchronizedWithCurrentItem="False"
XAML.cs :
lbResults.IsSynchronizedWithCurrentItem = false;

Should I use a Winforms combobox's SelectedItem, SelectedText, or SelectedValue?

I want to pass a value in a combo box as a param to a SQL statement. The Winforms combobox gives me several options for retrieving the value, namely SelectedItem, SelectedText, and SelectedValue. Which one is best/safest to use in this scenario?
SelectedValue is probably the best one to use
SelectedText will give you the selected text of the editable portion, Selected Item will return you the object and selected index will return you the index. Usually for applications SelectedValue is extracted and used.
Check out Combobox from MSDN
SelectedIndex Gets or sets the index specifying the currently selected item. (Overrides ListControl.SelectedIndex.)
SelectedItem Gets or sets currently selected item in the ComboBox.
SelectedText Gets or sets the text that is selected in the editable portion of a ComboBox.
SelectedValue Gets or sets the value of the member property specified by the ValueMember property. (Inherited from ListControl.)
if (comboBox1.DropDownStyle == DropDownStyle.DropDown ||
comboBox1.DropDownStyle == DropDownStyle.Simple)
{
return comboBox1.Text;
}
Text is probably the best one to use. This gets whatever is the currently selected text from the ComboBox as a string.
if (comboBox1.DropDownStyle == DropDownStyle.DropDownList)
{
return comboBox1.GetItemText(comboBox1.SelectedItem);
}
For this style, you cannot get the text from the ComboBox. This returns the string from the item at the currently SelectedIndex instead.
It depends on 3 things 1. Mode 2. DropDownStyle 3. Required Value
On ComboBox.SelectedIndexChanged
Unbound Mode
a. DropDownStyle = DropDown
SelectedItem will return = SelectedText
SelectedValue will return = ""
SelectedText will return = SelectedText
b. DropDownStyle = DropDownList
SelectedItem will return = SelectedText
SelectedValue will return = ""
SelectedText will return = ""
Use Data Bound Mode (Means you are populating your ComboBox from some data source i.e SQL Server Table)
You will select a Column of the table as DisplayMember and the same or another column as ValueMember.
a. DropDownStyle = DropDown
SelectedItem will return = System.Data.DataRowView (Prompt)
SelectedValue will return = Value of ValuMemeber
SelectedText will return = SelectedText (Value of DisplayMember)
b. DropDownStyle = DropDownList
.SelectedItem will return = System.Data.DataRowView (Prompt)
.SelectedValue will return = Value of ValueMember
.SelectedText will return = ""
Note: You can also use .Text that will return = Text of ComboBox
Conclusion:
Unboud Mode
.SelectedItem is the best choice
Data Bound Mode
a. ValueMember is required
.SelectedValue is the best choice
b. DisplayMember is required
.Text is the best choice
Microsoft suggest using this for value
ComboBox1.SelectedItem.ToString()
SelectedItem seems to be a safe choice.
I had this code:
NRBQConsts.currentSiteNum = listBoxSitesWithFetchedData.SelectedValue.ToString();
...which crashed with an NRE.
After changing it to this:
NRBQConsts.currentSiteNum = listBoxSitesWithFetchedData.SelectedItem.ToString();
...it works fine.

WPF TreeView ItemsSource not keeping values

I am using a wpf treeview and binding the ItemsSource to an IEnumerable of my ViewModel that has an IsChecked Property. I am binding a checkbox to this value with a Mode of TwoWay. I can see when I step through the program that it is setting this value properly on my ViewModel when I check the checkbox.
I then have a Menu Item that "Runs Checked". In this method I have a foreach loop that runs through the ItemsSource as IEnumerable of ViewModel looking for IsChecked = true to queue up the checked items to be run by a separate program. As such:
foreach (AccountViewModel account in tvClientList.ItemsSource as IEnumerable<AccountViewModel>)
{
if (account.IsChecked)
{
context.Queues.InsertOnSubmit(new Queue {Id = account.Id});
}
}
However, account.IsChecked is always false. Why is this?
i would check the following.
does the setter of AccountViewModel.Ischecked is called when clicking the treeview checkbox. if no - then thats the problem if yes you should look at your collection bindings.
debug your foreach and check wether the treeviewitemssource is the collection you expect.
maybe you can post your bindings, if that all no help.
ps: dont access your View controls directly if you use viewmodels/mvvm.
Just for anyone else encountering this issue. ItemsSource returns a new set of data, Items, however contains the current set of data.

Clearing selecteditem of listbox (which is bound to collection of objects) with MVVM

I have a listbox with silverlight 4. I have the list bound to a list of objects.
1.) The SelectedValue property is bound to a public property of the viewmodel called Current. How do i clear the selection? I have tried setting the value of Current to null. Well, this clears the selection however it also breaks binding in the edit form which has a combobox bound to a property of 'Current'. Textboxes which are bound to Current.FirstName etc. are working ok however the comboboxes do not function after I set the Current object to null.
2.) how do i load the form without the first item being selected?
Found a work around for this bug in Silverlight:
// Bug in SL listbox prevents SelectedIndex = -1 from unselected.
// Workaround is to use DispatcherBeginInvoke to do it async. Found
// work around here:
// http://sharplogic.com/blogs/rdavis/PermaLink,guid,2f5bbfa1-4878-490f-967d-bf00bc04dfde.aspx
Dispatcher.BeginInvoke(() => { QuickItemsListBox.SelectedIndex = -1; });
More details here:
http://sharplogic.com/blogs/rdavis/PermaLink,guid,2f5bbfa1-4878-490f-967d-bf00bc04dfde.aspx
Not sure when it was fixed but VoodooChild's answer works now in Silverlight 5. Passing this along in case others are looking.
yourCB.selectedIndex = -1;
Try:
yourCB.SelectedIndex = -1;

How to prevent a combobox from selecting an item from its list in wpf?

This combobox should display a bound value as its text, and cannot be disabled.
I just want to prevent a user from changing the displayed value.
comboBoxName.IsHitTestVisible = false;
comboBoxName.Focusable = false;
Use these two line codes together.
Edit Note: Edited to solve the problem described by skypecakes
Accoring to MSDN you need
IsReadOnly = true;
IsEditable = false;
See http://msdn.microsoft.com/en-us/library/system.windows.controls.combobox.isreadonly.aspx under Remarks
Edit: actually, I'm not sure any more
I suggest using a style to set ReadOnly to true on PART_EditableTextBox
Try in
XAML
IsEnabled="False"
C#
YOUCOMBOBOX.IsEnabled=false;
If you set the IsEnabled = false this should work. In your XAML it would look like this
<ComboBox IsEnabled="False"></ComboBox>
Your question is unclear.
A combobox has the dependency property IsEditable and if set to false, the selected item can not be edited. This is false by default.
If you are talking about the items in the combobox popup list, then it should also be "non editable" unless you have changed the data template of your combo box to contain a list of textboxes which will cause the list to allow each item to be editable.

Resources