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
}
Related
Hello stackoverflow community,
i am learning c# gui datagrid part
so i created a datagrid and added two column headers as shown below:
Now i have this value of a class
Program.AllStudents[Form1.userId].name;
Also i clicked on the datagrid and this function appeared
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
But I want my changes to appear once the form of datagrid is loaded not onclick
i want to add it for the name column,what should i do?
also the columns are editable so i want to know how to access them later
Thanks
You can use the DataGrid 'loaded' event like this:
xaml:
<DataGrid Name="dataGridView1"
Loaded="dataGridView1_OnLoaded"
>
c#:
private void dataGridView1_OnLoaded(object sender, RoutedEventArgs e)
{
dataGridView1.ItemsSource = Program.AllStudents[Form1.userId].name;
}
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;
}
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));
}
I have a Datagrid with some rows in it and I want to select corresponding rows, when a user selects a row. When I do this in the SelectionChanged-Event, the rows get selected (dataGrid.SelectedItems is set correctly), but this is not displayed in the GUI (only the row header is selected - not the row).
If I select the additional rows in the SelectedCellsChanged-Event, everything works as expected. Does anybody know, what is happening in between? Or better: What should be the correct way to do this?
Here is some code to reproduce (BTW: using .Net 4.5.1):
<DataGrid SelectionChanged="DataGrid_SelectionChanged" Name="myDatagrid"
ItemsSource="{Binding}" SelectedCellsChanged="myDatagrid_SelectedCellsChanged"/>
private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Does not display correctly
// myDatagrid.SelectedItems.Add(somerow)
}
private void myDatagrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
// Does display correctly
// myDatagrid.SelectedItems.Add(somerow)
}
I want to be able:
to open a mail when the user taps on one item.
and delete multiple emails when the user selects multiple emails
So I choosed LongListMultiSelector.
In built in LongListSelector, I handle the SelectionChanged event like this:
private void mails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedItem = mailsLongListSelector.SelectedItem as Mail;
if (selectedItem == null)
return;
...
mailsLongListSelector.SelectedItem = null;
}
I want exactly like that functionality in wptoolkit's LongListMultiSelector. like when you select an email to open and read it.
LongListMultiSelector's SelectionChanged occurs when you tap left side of an item and checkboxes appear. this is not what I want.
The Question is:
How can I perform something when the user taps on one item of LongListMultiSelector? thanks.
You could try this. If this is your LongListSelector
<tkit:LongListMultiSelector Name="longlist" SelectionChanged="longlist_SelectionChanged">
<tkit:LongListMultiSelector.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" FontSize="32" Tap="TextBlock_Tap"/>
</DataTemplate>
</tkit:LongListMultiSelector.ItemTemplate>
</tkit:LongListMultiSelector>
and it has an itemtemplate, you can detect a tap on item.
private void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var itemTapped = (sender as FrameworkElement).DataContext as Book;
}
and still have a selection changed
private void longlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
Once you are using LongListMultiSelector, the SelectionChanged event is fired when item is added or removed. If you want to perform the action regardless item is added/removed, I've managed to do it like this (for a simple string):
private void longlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string selectedItem = String.Empty;
if (e.AddedItems.Count > 0) selectedItem = e.AddedItems[0] as string;
else selectedItem = e.RemovedItems[0] as string;
MessageBox.Show(selectedItem); // do your work
}
It should run while items are selected separately by tapping, but this method will have problems when more items are added/removed at the same time - if you need it, then you should handle this also.