is there a e.ClickedItem for holding event - c#

Hi in my clicked event I am able to get the details of the button clicked with this
private async void BedGridView_ItemClick(object sender, ItemClickEventArgs e)
{
CommonVariables.PatientDetailsDict["bed_number"] = (e.ClickedItem as BedModelV2).bed_number;
}
I was wondering if there is something similar for my holding event so I could get the information of the selected item. As the code below does not work with HoldingState or originalsource.
private async void BedGridView_Holding(object sender, HoldingRoutedEventArgs e)
{
CommonVariables.PatientDetailsDict["bed_number"] = (e.OriginalSource as BedModelV2).bed_number;
}

I'm guessing you're using WP8.1 because GridView is supported in WP8.
To answer you question you can always get the SelectedItem of your GridView my referencing it by name or converting the sender object in the holding event like so:
<!-- define the gridview -->
<GridView x:Name="myGV" Holding="myGV_Holding"></GridView>
private void myGV_Holding(object sender, HoldingRoutedEventArgs e)
{
var selectedItem = this.myGV.SelectedItem; // reference by name
// var selectedItem = (sender as GridView).SelectedItem; // reference by converting the sender
}
Once you got the .SelectedItem it will be an object based off your ViewModel so then you can convert it back like you did in your original code.
CommonVariables.PatientDetailsDict["bed_number"] = (selectedItem as BedModelV2).bed_number;

Related

How do i get information that is object sender

I'm currently trying to get access to information that is passed in through an object sender.
The application I am working on is a winforms application with a list view. I want to get the number of the ListViewItem that the user has pressed on. The ListView item that I have pressed on is correct when I debug.
However I am unaware of how to get the information I want from the object sender. I want to access the ListViewItem number,
look at the posted image ListViewItem: {24919} in this case
so I can use this number as index, when I search in a database.
Does anyone have a fast tip so I can continue with my program ?
private void InvoiceListView_SelectedIndexChanged(object sender, EventArgs e)
{
//Connect to db and search based on the the listviewItemnumber.
}
Currently the object sender containsmethod;
You can type-check the sender and work with the result:
private void InvoiceListView_SelectedIndexChanged(object sender, EventArgs e)
{
if (!(sender is ListView listView)) return;
//work with the listView object from here:
listView.Items = ...
}
You can get the item selected by casting the sender to ListView and then get corresponding value as given below:
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
ListView lw = (ListView)sender;
foreach(ListViewItem lvi in lw.SelectedItems )
{
MessageBox.Show(lvi.SubItems[0].Text);
}
}

Keep Getting null on DataRowView'

private void OnDoubleClick(object sender, MouseButtonEventArgs e)
{
DataGrid dataGrid = (DataGrid)sender;
DataRowView row_selected = dataGrid.SelectedItem as DataRowView;
var s = row_selected["Nome"].ToString();
MessageBox.Show(s);
}
I'm trying to make a cell value message box when selected
I don't know what exactly you want to achive but I assume that your trying to pop a message box on double click on selected value on the grid view.
This is done by DataGridView Event.
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
}
Then you can use the DataGridViewCell.Value Property to retrieve the value stored in a particular cell.
MessageBox.Show(dataGridView1.SelectedCells[0].Value.ToString());
So the final code would look like this.
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show(dataGridView1.SelectedCells[0].Value.ToString());
}
NOTE change the Gridview name as you datagrid name

Multiple DropDownList but same action

I have two DropDownList with OnSelectedIndexChanged="SelectedIndexChanged" but I need to know in the C# code withch one is the one I used.
How can I know that?
Answering the questions:
I'm using Web Forms and I'm trying to change some GridViews Source from a choseen option in a DDL but the web had the same DDL (with different IDs) in several places and I can't delete them...
The general form for an event handler is:
OnSomeEvent(object sender, EventArgs e)
sender is a reference to the object that is raising the event.
In your case, sender is a reference to the DropDownList whose selected index has changed. So you should use something like this:
private void SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList the_list_that_changed = (DropDownList)sender;
int ids = the_list_that_changed.SelectedIndex;
}
The first parameter sender represents the object raising the event. Hence the Sender reference to your DropDownList whose triggered the selected index changed.
private void SelectedIndexChanged(object sender, EventArgs e)
{
if (((DropDownList)sender).ID == "firstDropDownID")
{
//To Do for first dropdown
}
else
{
//To Do for second dropdown
}
}

Metro GridView Mouse Over Which Binding ItemID

I can easily get the itemID of a datasource from the object that is clicked with the below code. But can anyone please tell me how to get the same ItemID when I use the Grid_PointerEntered(object sender, PointerRoutedEventArgs e) event?
I'm trying to play a different sound when you mouse over different Gridview items. the sound filename comes from the datasource item
Thanks.
void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
var itemId = ((SampleDataItem)e.ClickedItem).UniqueId;
//Do something here with the DatasetItem[ItemID]
}
OK I have tried this method
private async void Grid_PointerEntered(object sender, PointerRoutedEventArgs e)
{
String atext = "default";
Windows.UI.Xaml.Input.Pointer ptr = e.Pointer;
if (ptr.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse)
{
Windows.UI.Input.PointerPoint ptrPt = e.GetCurrentPoint(null);
TextBlock aTextBlock = Helpers.FindChild<TextBlock>(this, "aTittle");
}
The problem with this is the DataTemplate/Datagridview items all have the same name. As such the FindChild only returns the first item in the GridView.
take point location from the event arguments (see the sample here: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.input.pointerroutedeventargs.getcurrentpoint), then use VisualTreeHelper to check elements under the pointer
void GridView_PointerEntered(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
GridView gridView = sender as GridView;
FrameworkElement root = Window.Current.Content as FrameworkElement;
Point position = gridView.TransformToVisual(root).TransformPoint(e.GetCurrentPoint(gridView).Position);
// check items directly under the pointer
foreach (var element in VisualTreeHelper.FindElementsInHostCoordinates(position, root))
{
var context = ((FrameworkElement)element).DataContext;
}
}

selected item change ComboBox

Is there any way to prevent changing the selected item in a ComboBox only if for certain conditions? I want to allow update the selected item's displayValue in the ComboBox. But I don't want user to change the selected item when it's being updated. This is a windows application.
Inside your class:
private int _selectedIndex = 0;
Inside your form load method:
comboBox1.Enter += new EventHandler(comboBox1_Enter);
comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
Then the rest of the code:
protected void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
if (true) { // Add your validation or certain condition here.
(sender as ComboBox).SelectedIndex = _selectedIndex;
}
}
protected void comboBox1_Enter(object sender, EventArgs e) {
_selectedIndex = (sender as ComboBox).SelectedIndex;
}
Try setting set the Enabled property to false. (Or some third-party toolkits like Telerik have a ComboBox with a ReadOnly property.)

Categories

Resources