I have a problem with the combobox in WPF. You know that when you open the combobox and you start typing, that the selected index of the combobox is moving to the element that starts with the same letter. Well i actually need the same thing but a bit different.
The items in the combobox are actually binded to a class. This class has 2 properties, a Code property ( contains for example "XF15A") and a Description property ( contains for example "Radio"). I used a data template that actually binded the text for an combobox item to "[code] - [Description]".
Now when the type "XF" is goes to the combobox item that starts "XF". But what i now also need is that when you type "Ra" it should go to the combobox item "XF15A - Radio".
Do you guys know how to solve this? I'm also open for existing usercontrols.
Thanks,
My code is not quite what you want, but should give you an example of how you could do it yourself:
You got to handle PreviewTextInput yourself and let your algorithm decide which item to select. Here's a simple example:
XAML:
<ComboBox x:Name="cb" PreviewTextInput="ComboBox_PreviewTextInput">
<ComboBoxItem>adsfsf</ComboBoxItem>
<ComboBoxItem>adsfsf</ComboBoxItem>
<ComboBoxItem>acdd</ComboBoxItem>
<ComboBoxItem>adsfsf</ComboBoxItem>
</ComboBox>
Code Behind:
private void ComboBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
cb.IsDropDownOpen = true;
foreach (ComboBoxItem item in cb.Items)
{
var str = (string)item.Content;
if(str.Contains(e.Text))
{
cb.SelectedItem = item;
break;
}
}
}
Related
I am from Iran and I cant speak English very well, sorry.
I made something like OpenFileDialog in WinForms
and work correctly.
After, for better user interface, I tried to make it in WPF.
I use TreeView and other controls for it in both platforms (Winforms and WPF)
in Winforms I could do this correctly usingbelow code:
private void Folder_FileTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
Folder_FileTreeView.Nodes.Clear();//this is necessary to clean first page node, after get new folders
if(e.Node.Text=="Desktop")//also this code is necessary to compare node
{
//Do something
}
}
Also in WPF I can get text of Item by below code:
private void Folder_FileTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
if (e.NewValue!=null)
{
StackPanel CustomStackPanel = (StackPanel)((TreeViewItem)e.NewValue).Header;
TextBlock textBlock = (TextBlock)CustomStackPanel.Children[1];
nodetext = textBlock.Text;//this line return text of item for compare
}
Folder_FileTreeView.Items.Clear();
}
If I don't use Folder_FileTreeView.Items.Clear() the above code return folders without clearing first page, but if I do use Folder_FileTreeView.Items.Clear() e.NewValue returns null.
Please help me to use together these codes: Folder_FileTreeView.Items.Clear();(or clear first page) and get text of selecteditem by user without return null
Thanks A lot
e.NewItem will be null if the TreeView used to have an item selected but now does not. When you clear the items, you are removing any selection, this of course changes the selection and raises the SelectedItemChanged event with null as the new selection- since there are no possible items that could be selected.
If you want to replace the items in the list with new items after the user makes a selection, the selected item will be null while that change is happening. You need to do the following:
Handle the SelectedItemChanged event and remember the new selected item in a variable.
For example, if they click on the item for "Desktop" set a variable (e.g. Path) to the path for the user's desktop (e.g. C:\Users\UserName\Desktop).
Clear the list of folders in the TreeView. This will trigger SelectedItemChanged again, but you want to ignore it this time because e.NewItem == null.
Read all the folders in Path and make new items for each of those folders.
The way was found by below code
private void Folder_FileTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
Folder_FileTreeView.SelectedItemChanged -= Folder_FileTreeView_SelectedItemChanged;
if (e.NewValue!=null)
{
StackPanel CustomStackPanel = (StackPanel)((TreeViewItem)e.NewValue).Header;
TextBlock textBlock = (TextBlock)CustomStackPanel.Children[1];
nodetext = textBlock.Text;//this line return text of item for compare
}
Folder_FileTreeView.Items.Clear();
Folder_FileTreeView.SelectedItemChanged += Folder_FileTreeView_SelectedItemChanged;
}
thank very much for every one helped me
I am having trouble with a databound datagridviewcomboboxcell
I want to remove the blue selection line that appears on a databound comboboxcell.
I noticed that if a comboboxcell is not databound but has a collection of items, the blue line does not appear. however a databound combobox does have it.
item collection
Databound
You will see in the first picture there is no blue selection line however in the next picture(databound comboboxcell) there is...
I need to take this selection line away so that when the databound comboboxcell has only one row of data, a user can through only keyboard inputs can make a selection.
I initially tried to add a keyDown event to set the Items[index] which did change the value, however, when i move off the cell, it displays the Model Name and namespace. Then when going back on to the cell it displays the value.
I used the following code to do this:
I added a keydown event to the combobox, and here is the keydown event
private void dataGridView_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].CellType.Name == "DataGridViewComboBoxCell" && dataGridView1.CurrentCell.ReadOnly == false)
{
DataGridViewRow row = dataGridView1.CurrentRow;
try
{
if ((row.Cells[dataGridView1.CurrentCell.ColumnIndex] as DataGridViewComboBoxCell).Items.Count == 1)
{
(dataGridView1.CurrentRow.Cells[dataGridView1.CurrentCell.ColumnIndex] as DataGridViewComboBoxCell).Value = taxcodes[0];
(dataGridView1.CurrentRow.Cells[dataGridView1.CurrentCell.ColumnIndex] as DataGridViewComboBoxCell).DisplayMember = "FullDescription";
(dataGridView1.CurrentRow.Cells[dataGridView1.CurrentCell.ColumnIndex] as DataGridViewComboBoxCell).ValueMember = "TaxID";
}
}
catch
{
}
}
}
Now that I have attempted to change set the value of the combobox without luck, I am on to the next possible solution i can maybe get to work.
If i can make the Combobox have a no selection line when it opnes initially then as soon as that line moves on to the only item in the list it will select give the ability to select that as the value.
NOTE: a databound comboboxcell with more than one items, works well
NOTE: a non-databound comboboxcell with items defined works well, however i need a displaymember as well as value member
The ideal outcome from this query will give me the ability to select a databound combobox item(making use of the ENTER key) when the combobox has only one item.
Final NOTE: when i use the mouse to make the selection on the databound comboboxcell with ony 1 item, it works perfectly.
Thanks for the help if anyone can help
So after consulting a friend, I managed to override the .ToString() method
public override string ToString()
{
return FullDescription;
}
This worked for me in this case and the Keydown route was the correct route to go.
Hi I have a ListBox and set it up as follows in xaml:
<dxdo:LayoutPanel Caption="Raw Data File Names" ItemWidth="2*">
<ListBox ItemsSource="{Binding FilteredFileNames}" SelectionMode="Extended" SelectionChanged="Selector_OnSelectionChanged"/>
</dxdo:LayoutPanel>
When I handle the event in code-behind each time I select multiple items (via shift-down plus mouse click) I noticed that the first item is never included in the array of items:
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var vm = DataContext as HistoricalDataImportRawDataViewModel;
vm.SelectedFileNames = e.AddedItems.Cast<string>().ToList();
}
What am I doing wrong? Is it because AddedItems only include the items beyond the initial selection? What can I do to get the complete collection of items? Please note that I have to use SelectionMode="Extended". Is the omission of the first item intended or a bug?
The AddedItems property tells which item was added to the selected items. If you are interested about all the items that are selected, you have to access ListBox property SelectedItems.
var listbox = (ListBox) sender;
var selectedItems = listbox.SelectedItems
.Cast<string>()
.ToList();
At the moment I have method I created so that when you click anything in the Treeview, the method will activate.
private void MyTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
cAuditTasksEntity task = new cAuditTasksEntity();
cAuditTasksEntity entityTask = MyTreeView.SelectedItem as cAuditTasksEntity;
}
This is my To-Do-list, When they select something from anything in the list of _Pot which on the picture includes Acceptance Pot 1 Acceptance Pot 2, I need it to return that SelectedItem.
With that SelectedItem in a variable I can get the PolicyNumber and search the database for that Task(the SelectedItems) details.
EDIT:
I have added this code:
var Info = MyTreeView.SelectedItem;
I know it will do any SelectedItem in the TreeView but I can add an IF statement around it, this reads the Date & PolicyNumber from the picture I shown with that how can I get the PolicyNumber and find the TransactionType that matches that Policynumber.
One solution would be to create a SelectedItem property on your view model. You can then bind your SelectedItem to this property in xaml:
<... SelectedItem={Binding SelectedItem} />
You can then access this within the method you have defined.
I have a program with a class called MyClass and Location. MyClass contains an ObservableCollection of Location items and Location contains a string property called Name. In MainPage.xaml I have a LongListSelector (with a ContextMenu for each item) populated with grids representing a Location.
When I click the 'remove' menu item from the context control, it will usually remove the underlying Location object and update the view. After a few cycles of populating the LongListSelector and removing all its items, some new items that are added can't be removed anymore.
Here's an example of what I mean: The LLS originally contains 2 items. Then I delete those 2 items and add 3 more. However, I can only remove the third one, in this case, but not the first 2.
Here's the ContextMenu MenuItem click event from MainPage.xaml.cs:
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
var selectedItem = (sender as MenuItem).DataContext as Location;
for (int i = 0; i < MyClass.Locations.Count; i++)
{
if (MyClass.Locations[i].Name == selectedItem.Name)
{
MyClass.Locations.Remove(MyClass.Locations[i]);
break;
}
}
}
Prior to using a for loop, I used this LINQ code and still had the same problem:
var toRemove = MyClass.Locations.Where(x => x.Name == selectedItem.Name).SingleOrDefault();
MyClass.Locations.Remove(toRemove);
Any suggestions to fix this problem?
I suggest you to use a ListBox instead of LLS - if you are not using grouping option. It works much better and causes less problems.
By the way I've also encountered some problems with this Control - maybe similar to yours. Weird is also that LLS.UpdateLayout() doesn't work while in ListBox works perfect.