I'm building a wpf application and I made my database using Entity Framework code first.
I have a list box bound to a table
XAML
<ListBox x:Name="lstDishes" Height="415" VerticalAlignment="Bottom" Margin="0,0,1032,42" HorizontalAlignment="Right" Width="281" ItemsSource="{Binding}" Background="#FFDDC9B0" BorderBrush="{x:Null}" FontSize="15" MouseDoubleClick="lstDishes_MouseDoubleClick">
XAML.CS
var dish = (from Dish in db.Dishes
select Dish).ToList();
lstDishes.ItemsSource = dish;
lstDishes.DisplayMemberPath = "Description";
On item double click, I would like to show selected item in another listbox.
I was trying to manage it, but before I was doing some kind of trial for the selected item, showing a message box on double click
private void lstDishes_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("You selected: " + lstDishes.SelectedItem.ToString());
}
But the result of my messagebox is this:
Of course it's not readable.
How can I get the selected item as it's shown in listbox?
Do you have any suggestion for the next step where I'm going to binde the selected item to another listbox?
Edit
Since my listbox is bound to a table in database, I had to cast the selected item as my type of entity.
You need to get the type of the selected item and
private void lstDishes_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var Selected = lstDishes.SelectedItem as Dish;
MessageBox.Show("You selected: " + Selected.Description));
}
Related
Is it possible to add an row with an searchbox foreach column in an UWP DataGrid so I can filter the results by text on that column like in an Excel table?
Is it possible to add an row with an searchbox foreach column in an UWP DataGrid so I can filter the results
Currently, there is no such table to make filter directly, and insert new searchbox as a row into DataGrid is hard to implement.
For your scenario, we suggest you make ComboBox in Top of DataGrid, and integrate the property that you want to filter as ComboBox's datasource. Then use the select value to filter your DataGrid.
private ObservableCollection<Item> temp;
private void SearchBox_Loaded(object sender, RoutedEventArgs e)
{
SearchBox.ItemsSource = MyClasses.OrderBy(x => x.Name).Select(x => x.Name).Distinct().ToList();
temp = MyClasses;
}
private void SearchBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MyDataGrid.ItemsSource = new ObservableCollection<Item>(from item in temp
where item.Name == SearchBox.SelectedValue.ToString()
select item);
}
Xaml
<ComboBox
x:Name="SearchBox"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Loaded="SearchBox_Loaded"
SelectionChanged="SearchBox_SelectionChanged"
/>
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;
}
}
}
I have 2 comboboxes each with 2 methods for _Loaded and _Selection changed
i want to select a location in the first combobox and then the next combobox should list a bunch of dates for that specific location
Here is what i have so far:
<ComboBox
x:Name="comboBoxLocation"
Text="Lokation"
HorizontalAlignment="Left" Margin="50,305,0,0"
VerticalAlignment="Top"
Width="120"
Loaded="ComboBoxLocation_Loaded"
SelectionChanged="ComboBoxLocation_SelectionChanged"/>
<ComboBox x:Name="comboBoxDate"
Text="Dato" HorizontalAlignment="Left"
Margin="195,305,0,0"
VerticalAlignment="Top" Width="120"
Loaded="ComboBoxDate_Loaded"
SelectionChanged="ComboBoxDate_SelectionChanged"/>
and
private void ComboBoxLocation_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var comboBoxLocation = sender as ComboBox;
comboBoxDate.SelectedIndex = 0;
comboBoxDate.ItemsSource = controller.GetBusTimes();
//ComboBoxDate_Loaded(sender, e);
}
private void ComboBoxDate_Loaded(object sender, RoutedEventArgs e)
{
List<string> dataDate = controller.GetBusTimes();
var comboBoxDate = sender as ComboBox;
comboBoxDate.ItemsSource = dataDate;
}
This seems to be a lot more difficult than i expected... I am starting to think that i might have madesome fundamental mistake here...
I have been fiddling around with this... I can manage to show a list of locations in the first box and the relevant dates for that location the second box. But when i change the first location, the dates stay the same...
How would i go about this?
There are 2 issues with your code.
It is not clear where & how controller.GetBusTimes() gets information about changed location?
If somehow the above-mentioned function is in know of location change and is still not showing updated info in another combo box then
See also this answer on how to refresh combo box display once ItemSource is changed
Why not to bind separately Item source and selected item?
<Combobox ItemsSource="{Binding ItempsProperty}" SelectedItem="{Binding StrValueProperty, Mode=TwoWay}" />
And you could set one property in the setter of other
Try this, I have used this to link 2 comboboxes in my windows form application.
By default set both comboboxes selection to 0 //Combobox1.SelectedIndex = 0;
private void combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
var combobox1VALUE= combobox1.Text;
}
private void combobox2_DropDown(object sender, EventArgs e)
{
//Select datasouece according to combobox 1 data selection(combobox1VALUE)
combobox2.Items.Clear();
//add data to combobox2
}
The Problem: I cannot enter custom text to the ComboBox and press Enter to close the dropdown list, because my written custom-text is overwritten by the selected item from the dropdown list.
I use an editable=true and isTextSearchEnabled=true ComboBox with a list of strings:
<ComboBox
IsEditable="True"
IsTextSearchEnabled="True"
ItemsSource="{Binding Names}"
SelectedItem="{Binding SelectedName}"
Text="{Binding Name}"
>
<ComboBox.Style>
<Style>
<EventSetter Event="TextBoxBase.TextChanged"
Handler="cmbTextField_TextChanged" />
</Style>
</ComboBox.Style>
</ComboBox>
TextChanged: Opens the combobox dropdown list if the text is changed
private void cmbTextField_TextChanged(object sender, TextChangedEventArgs e)
{
var cmbx = sender as ComboBox;
//Open the dropdwon
cmbx.IsDropDownOpen = true;
}
How-to get the problem:
Enter the first letter e.g.: "A". -> it opens the dropdown list and selects the first Name which starts with A.
Type some additional letters to the end of the found name (to get a new string which is not in the list)
Press Enter -> dropdown window is closed and my custom text is overwritten with the selected text from the list.
(But it is working correctly if I press TAB instead of Enter)
Does anyone know how to solve this issue?
Update:
The problem seems to be related to the IsTextSearchEnabled=true property.
You can trying subscribing to the OnPreviewKeyDown method, which will fire before the key press is processed. When the method is called you can check if the key pressed was return and mark it as handled.
Something along this lines should do it:
private void cmbTextField_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
if (e.KeyData == Keys.Return) {
e.Handled = true;
}
}
Bear in mind this code wasn't tested.
I know that's a long time since the post but I solved it by register to event: SelectionChanged and close the dropdown menu:
private void Combobox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox cb = sender as ComboBox;
cb.IsDropDownOpen = false;
}
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();