Combobox automatically dropdown - c#

I have editable combobox, MVVM.
I need dropdown=true when I write something in combobox.
Text="{Binding textsearch, UpdateSourceTrigger=PropertyChanged}"
here in textsearch I wrote OnPropertyChange for ComboBox IsDropDownOpen=true, but it works only onсe, when I select row, then try to write again, the dropdown=false and not react to property...
Also, when I selected row and change text, I can't select this row again, because it was selected, I need change selected item first, but when I change selected Item, the text changes too.
How to make filter works?
Or like another variant, I added textbox for filter text, but when I write text and call textsearch property, the textbox lost focus and combobox has this focus... how to save focus on textbox and dropdown combobox itemslist?

The easiest way to do something like that is to handle one or more events. You could try to handle the PreviewTextInput event:
<ComboBox ItemsSource="{Binding Items}" IsEditable="True"
PreviewTextInput="ComboBox_PreviewTextInput" />
...
private void ComboBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
ComboBox comboBox = sender as ComboBox;
comboBox.IsDropDownOpen = true;
}
The code in this event handler will open the drop down each time a user types into the ComboBox TextBox. You may need to handle a few more events to get your exact desired behaviour, but you can see which events are available from the ComboBox Events page on MSDN and complete this yourself.

Related

DevExpress DataGrid filter cell triggers SelectedItem change

I have a DevExpress WPF datagrid and the SelectedItem is used via Binding in the ViewModel.
When I select a row, it loads data in a detail view.
However, when I select a filter cell, it also triggers the SelectedItem binding and it tries to load...well, nothing as there is no real row selected.
I intend to use the filter as the following: the SelectedItem is the first item in the filtered list.
How can I prevent the filter cell from triggering the SelectedItem change and make my first item as the selected one?
You need to set the SelectedItem's Binding with an
UpdateSourceTrigger=PropertyChanged
the default UpdateSourceTrigger for all binding's inside a DataGrid is LostFocus.
that's why your item is updated when leaving the row , it lost it's focus.
Please check syntax since it was not written in VisualStudio .
<ComboBox SelectedItem="{Binding YourItem,UpdateSourceTrigger=PropertyChanged}" />

C# show property in textbox depending on combobox selecteditem

I have maybe an easy question but I would like to ask about possibilities how to bind textbox Text property to combobox SelectedItem property. I do it through combobox SelectedItemChanged event and set text like this:
if(cmbMeasuring.SelectedItem != null)
txtMethod.Text = ((ListBoxItem)cmbMeasuring.SelectedItem).Value;
I have class ListBoxItem which holds 2 strings "Name" and "Value". Then I created BindingList for combobox:
private BindingList<ListBoxItem> lst;
and then set combobox data source in constructor:
cmbMeasuring.DataSource = lst;
cmbMeasuring.DisplayMember = "Name";
This works fine but I dont know if its the best way how to do it. But problem occurs when I change the textbox content. I do it through textbox Leave event:
private void txtMethod_Leave(object sender, EventArgs e)
{
if (cmbMeasuring.SelectedItem != null)
((ListBoxItem)cmbMeasuring.SelectedItem).Value = txtMethod.Text;
}
If textbox lost focus I assign item value. But I have also a menustrip to save input and when I click to it directly this event dont occur so the last input is not saved. I know that this could be done through textbox TextChanged event but it consume a lot of time.
Do you have any better solutions or is it OK? Im not using WPF.
Thanks.
If you have a Click event for the MenuStrip item, you can do the following
MyMenuStripItem.Focus();
This should cause the MenuStrip item to gain focus and therefore causing the TextBox to lose focus.
Try data binding on the TextBox in your form's constructor:
txtMethod.DataBindings.Add("Text", lst, "Value",
false, DataSourceUpdateMode.OnPropertyChanged);

How to get tapped item in gridview

I want to get the tapped item in my gridview, but I can't use itemclick event with some reason, after get the item, I also want to convert it as an object which the item hold. The gridview had DataTemplate, when I see the e.OriginalSource value in tapped event, it is a TextBlock. Anyone can help?
I answered a similar question here:
Windows 8 Metro: How to get clicked GridViewItem?
If you're trying to get the GridViewItem object for the item the user clicked on, this can be captured by the GridView's ItemClick event by using the GridView's ItemContainerGenerator.ContainerFromItem method.
Step 1: Enable IsTapEnabled="True" and Create Tapped event in Gridview
<GridView x:Name="categoryItemsGV"
Margin="5,5,0,0"
IsItemClickEnabled="True" IsTapEnabled="True" Tapped="categoryItemsGV_Tapped"
SelectionMode="Single"
Step 2: This GridView bind with TableModel list, and below Tapped Event
private void categoryItemsGV_Tapped(object sender, TappedRoutedEventArgs e)
{
var selectedTableModel= (TableModel)(sender as GridView).SelectedItem;
}
This selectedTableModel contain selected items model.
You can use "onItemInvoked" event of control ListView as below code:
//item click handler for list view
document.getElementById("yourListViewId").addEventListener("iteminvoked", function(e) {
e.detail.itemPromise.then(function (invokedItem) {
WinJS.Navigation.navigate("url to another pages", { objectToTransfer: invokedItem.data });
});
});
Using "invokedItem.data" to pass whole object data which ListViewItem holds
You can try using attached behavior aka WinRT Behavior and use it to relay tapped event to a command in your view model
The DataContext of your TextBlock is what you want.

How do I check if a selected item has changed from the previously selected item?

I have a listbox in my winform, when an item in the listbox is selected, a value is placed into a textbox in the same Form. There my many items in my listbox which when selected, i want the text box to be empty so i can pass in the new value. How do i check is if the user has clicked on something other their initial selected item? i get the currently selected item like this below:
var selectedItem = (ReportItems)listbox.selectedItem
You can use the SelectedIndexChanged event on your ListBox . You can create an event handler for this event to determine when the selected index in the ListBox has been changed. This can be useful when you need to display information in other controls based on the current selection in the ListBox. You can use the event handler for this event to load the information in the other controls.
See MSDN documentation: link
You can add a global variable for your ReportItems and call it 'selItem'.
After the user changed the selected Item you check the "new" selectedItem with the 'selItem'-variable.. i don't think that a listbox has a method that can check if the selection has changed from the previous one..
I'm not sure if there is a reason you're not leveraging the SelectionChanged event of the ListBox or not but you should be if you're not. Further, determining if it's different than the initially selected item should be pretty straight forward because you can save the initially selected item in a private variable in your form and compare every time the method handler for SelectionChanged fires.
Other than this, there's not much more I can suggest because your questions isn't terribly clear and there is no code to look at.
My solution was to always clear the textbox first. So as soon as a user selects an item in the listview, rather than populating the textbox straight away, clear the textbox before populating it.
clearText(); is called soon as a listbox item is clicked.
public void clearText()
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
textBox7.Clear();
textBox8.Clear();
textBox9.Clear();
textBox10.Clear();
textBox11.Clear();
textBox12.Clear();
}

How to select and deselect all items in a listbox using radio buttons in C#

I want to select all the items in listbox. Here iam using listbox1.selectAll() for selecting all items. And for Deselecting all items in a listbox iam using listbox1.selecteditems.clear(). thats working perfectly
Now i want to do validations like if i select all items by using listbox1.selectAll() and then if i select one item in listbox all selected items selection is going off and the radio button still showing the selectall is checked.But i dont have all items selected in a listbox. How to do that. Any suggestion plz.
I would suggest using a single CheckBox or two standard Buttons instead of RadioButtons.
If some but not all or none of the items are selected, which RadioButton will you check? It doesn't make sense to have a "Some selected" RadioButton.
With buttons, you simply select/unselect all of the items when the button is pressed.
With a single CheckBox, you can use the three state feature to set the CheckBox as follows: checked = all selected; third state = some selected; unchecked = none selected.
Handle the appropriate Checked and Unchecked handlers on the CheckBox for updating the ListBox, and respond to the ListBox.SelectionChanged event to update the CheckBox in response to manual selection changes.
You could implement a check in the ListBox1_SelectedIndexChanged event to do a check against the checkbox i.e.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
myCheckBox.Checked = listBox1.SelectedItems.Count > 1;
}

Categories