In the example below, when i try to set values to the combobox, the event is triggered and change objRunSettings.xxx before the value is set to cmbxxx.SelectedValue.
I suppose i need something like SelectionChangedCommited instead of SelectionChanged but i am really confused how to do it, as in xaml my only option is SelectionChanged
In xaml
<ComboBox SelectionChanged ="cmbxxx_selectionChanged"/>
In .cs -> set combobox values
cmbxxx.SelectedValue = objRunSettings.xxx;
Event
private void cmbxxx_selectionChanged(object sender, SelectionChangedEventArgs e)
{
objRunSettings.xxx = cmbxxx.SelectedValue.ToString();
}
The first item in the e.AddedItems IList will be the value you are looking for.
MSDN SelectionChangedEventArgs
Related
the event is triggered when the Form with the DataGridView is initialized, when the column headers texts are placed. So the function handling the event runs at start-up as many time as there are columns. How can I stop that and have it triggered only after the user made some changes in the datagrid?
Added:
It seems that changing from CellValueChanged to CellEndEdit does the trick.
Have you tried using the SelectionChanged property of the datagrid, triggering an event when the content changes?
<Grid>
<DataGrid> Name="TestDataGrid" SelectionChanged="DataGrid_EventName" />
</Grid>
Within the Code-Behind, the event will be captured:
private void DataGrid_EventName(object sender, SelectionChangedEventArgs e)
{
//Conduct work here
}
Edit: I'm quite sure that you will still need to initialize the DataGrid.
For example, you can use this event to extract the row-specific information from within the DataGrid:
private void DataGrid_EventName(object sender, SelectionChangedEventArgs e)
{
DataGrid _dataGrid = (DataGrid)sender;
DataRowView selectedRow = _dataGrid.SelectedItem as DataRowView;
if (selectedRow != null)
{
RowItemList = new List<string>(); //List declared outside of this method...
//add row information to the list
RowItemList.Add(selectedRow.ToString());
//or, get column-specific information
string ColmnEntry = selectedRow[ColumnName].ToString();
}
}
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;
}
Currently I am working on multiselect combobox custom control with combobox inherited, which attached a listbox to a combobox with a multiple selection mode. But now I face a problem when passing the selected items to my custom control in view. I use the listbox selectionchanged event to update my selected item in my combobox. I'm able to implement this approach on selecteditem, it manages to pass one selected item to my view. But I want to pass all the selecteditems to my view.
And this is what I have done so far.
MultiSelectCombobox.xaml <-- My custom control xaml inherited from combobox
<ComboBox.Template>
...
<Popup>
<ListBox SelectionMode="Multiple" ItemsSource="{TemplateBinding ItemsSource}"
SelectionChanged="ListBox_SelectionChanged">
...
</ListBox>
</Popup>
MultiSelectCombobox.xaml.cs
public partial class MultiSelectComboBox : ComboBox
...
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox lb = sender as ListBox;
this.SelectedItem = lb.SelectedItem;
}
MainWindow.xaml.cs
MultiComboBox mComboBox = (sender as MultiComboBox);
MessageBox.Show(this, mComboBox.SelectedItem.ToString());
use this 2 line in some combobox event to display selecteditem.
I search through few posts, but I'm still unable to find any solution, I would appreciate if you could provide some guidance for me.
Try this...
MultiSelectCombobox.xaml.cs
public partial class MultiSelectComboBox : ComboBox
{
...
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems", typeof(IList),
typeof(MultiSelectComboBox));
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox lb = sender as ListBox;
this.SelectedItem = lb.SelectedItem;
this.SelectedItems = lb.SelectedItems;
}
}
Keep in mind that this would only work for one way selection. If you want to make sure it is only used to get the selected items and that nobody tries to set them this way, you could turn the DependencyProperty into a readonly one and use an IEnumerable instead of an IList.
If you wanna support two way selection changes (changing the selected items from code and not only from user interaction with the ListBox) then you'd have to add a property changed callback to the DependencyProperty and maybe use an ObservableCollection to listen for changes in the collection, and update the ListBox selection accordingly.
how can I get the clicket elevent from a ListView which has the IsItemClickEnabled enabled?
I know how to get the selected Item/Index but not the clicked item.
ItemClick is working but I can not say s.th. like:
Object selection = listView1.SelectedItem;
EDIT:
I have a ListView and I need to catch the clicked item from this list in the following method:
private void listView1_ItemClick(object sender, ItemClickEventArgs e)
{
...
}
I may be missing something, but doesn't the following work for you?
private void lv_ItemClick_1(object sender, ItemClickEventArgs e)
{
var item = e.ClickedItem as String;
}
Here I assume the items in the list are simple strings, but in general they'll be whatever type you are using in the collection you've bound to the ItemsSource property of the ListView.
I guess you can also try the SelectionChanged event, and get the clicked item as e.AddedItems or MyListView.SelectedItem or MyListView.SelectedItems.
how to do: what i pick on combobox1 will show on combobox2 ?
is it possible ? (in C#)
thank's in advance
You need to subscride second combobox on the SelectedIndexChanged event of the first combobox and change value when event triggers. Also you need to make sure that both combobox have several items or you will need to add missing items to second combobox dynamically.
Event handler example:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
object selectedValue = this.comboBox1.SelectedValue;
this.comboBox2.SelectedValue = selectedValue;
}