C# ListView - control checkBox event - c#

Suppose I need to change the status of an item from active = true to active = false and vise versa and at the same time persist my change in the database table.
I tested ItemChecked event like the following:
private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
{
ListViewItem item = (ListViewItem)sender;
if (item != null)
{
Book b = (Book) item.Tag;
b.MakeActive(item.Checked);
}
}
I failed.
Can anyone help me?

in this case object sender is ListView and not ListViewItem your code should be this
private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
{
ListViewItem item = e.Item as ListViewItem;
if (item != null)
{
Book b = (Book) item.Tag;
b.MakeActive(item.Checked);
}
}

Related

ButtonClick to get an Object on SelectionChanged event

I have a SelectionChanged event and works perfectly, but I want to figure out how to "catch" this selected item at the click of a button they need to pass it as parameter to another page and edit this Item. Here's the current code and button SelectionChanged I still implemented because this is what I need.
private void listCarros_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
ListBox listBox = sender as ListBox;
if (listBox != null && listBox.SelectedItem != null)
{
//pega o Carro que foi selecionado
Carro sCar = (Carro)listBox.SelectedItem;
btnEditCar.IsEnabled = true;
btnDeleteCar.IsEnabled = true;
}
else
{
btnEditCar.IsEnabled = false;
btnDeleteCar.IsEnabled = false;
}
}
I need to edit the selectedItem on this button:
private void btnEditCar_Click(object sender, EventArgs e)
{
//Here I need access to the selectedItem on SelectionChanged event.
}
If you could also tell me how to pass the object as parameter would be perfect.
You can do this with binding also
1.Bind ListBoxItem(Carro Object) to the tag of "btnEditCar" in xaml.
Xaml should be like this
<Button Name="btnEditCar" OnClick="btnEditCar_Click" Tag="{Binding}"/>
and now in
private void btnEditCar_Click(object sender, EventArgs e)
{
Carro sCar=(Carro)((sender as FrameworkElement).Tag)
}
This is the good practice,creating a class variable only for temporary purpose is hack
To give a better idea on my comments. Creating a class level variable is like this:
Notice that sCar is declared outside the method, but within the class.
Carro sCar = new Carro();
private void listCarros_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
ListBox listBox = sender as ListBox;
if (listBox != null && listBox.SelectedItem != null)
{
sCar = (Carro)listBox.SelectedItem;
...
private void btnEditCar_Click(object sender, EventArgs e)
{
sCar.ProperyYouWantToChange = "Stuff I want to change"
}

Devexpress DataGrid contextmenu disappear

I have a strange problem with DataGrid context menu. I load item details from server after click on a item in datagrid. While loading the details the application shows a waitscreen:
private void gridViewOrders_MouseDown (object sender, MouseEventArgs e)
{
GridView gv = sender as GridView;
if (gv != null)
{
ShowWaitScreen (message);
GridHitInfo ghi = gv.CalcHitInfo (e.Location);
...
CloseWaitScreen ( );
}
}
When the user click the right mouse button, it should shows a context menu:
private void gridViewOrders_PopupMenuShowing (object sender, PopupMenuShowingEventArgs e)
{
if (e.MenuType == GridMenuType.Row)
{
DXMenuItem item = new DXMenuItem ("Delete", OnBtnDeleteOrder_Click);
e.Menu.Items.Add (item);
}
}
But the menu disappear at once. When I remove the waitscreen, the context menu is shown and the user can select the "Delete" menuitem. Any hints, how I can fix this problem? Thank you!
A good solution for my problem is to do the following:
private void gridViewOrders_MouseDown (object sender, MouseEventArgs e)
{
GridView gv = sender as GridView;
if (gv != null)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
ShowWaitScreen (message);
...
CloseWaitScreen ( )
}
}
}
Thanks to the DevExpress team for the hint!
I dont really thisnk you need to handle anything in MouseDown event.
This code works for GridView:
private void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
{
GridView view = sender as GridView;
if (e.MenuType == DevExpress.XtraGrid.Views.Grid.GridMenuType.Row)
{
int rowHandle = e.HitInfo.RowHandle;
e.Menu.Items.Clear();
DXMenuItem zaznaczItem = new DXMenuItem("Zaznacz wszystkie", new EventHandler(zaznacz_Click));
DXMenuItem odznaczItem = new DXMenuItem("Odznacz wszystkie", new EventHandler(odznacz_Click));
e.Menu.Items.Add(zaznaczItem);
e.Menu.Items.Add(odznaczItem);
}
}
void zaznacz_Click(object sender, EventArgs e)
{
foreach (DataRow dr in (gcKontrahent.DataSource as DataTable).Rows)
{
dr["checkbox"] = true;
}
}
Handler zaznacz_Click is just example of handler for selected menu item. odznacz_Click is similar so I didnt post it. I dont have example for DataGrid so excuise me if it's not good solution. Just take it as example for acomplishing context menu handling in GridView.

object sender listviewitem .text property

I am trying to access listviewitem selectitem text property via object sender. how do I do this?
private void button1_Click(object sender, EventArgs e)
{
ListViewItem LS = new ListViewItem(#"c:\windows\explorer.exe");
ListViewItem LS1 = new ListViewItem(#"c:\windows\notepad.exe");
listView1.Items.Add(LS);
listView1.Items.Add(LS1);
}
private void listView1_Click(object sender, EventArgs e)
{
// ???? how do I get listviewitem.text property here based on item selected
// ?? am i using the right eventhandler?
}
You can just use the SelectedItems property:
if (listView1.SelectedItems.Count > 0)
listView1.SelectedItems[0].Text // the text of the first selected item
Access it simply by using the ListViewitself
myListView.SelectedItems[0].Text

WP7 listbox binds to second observable collection autoselection error

I have a list box which is bound to a Service. It loads fine and allows me to select an item and moves through to second page.
The Second page loads a new observable collection from the same service and uses the selected Item from previous page to load a new listbox with a data filter.
This second listbox allows for a further selection to load a details page based on this selection. The error I have is that the second listbox loads and automatically selects the first item in the list. Upon pushing the back button it loads the second listbox with no selection made.
Can someone please tell me how to stop the automatic selection in my second listbox? Code details below.
First Listbox code including sender
void ServiceReference1Client_GetMensleaderListCompleted(object sender, GetMensLeaderListCompletedEventArgs e)
{
if (e.Error != null) { MensHeading.Text = "Connect to Data"; return; }
ObservableCollection<Mens_Leaders> mensLeaders = e.Result;
this.mensRankings.ItemsSource = mensLeaders;
}
private void mensRankings_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (mensRankings.SelectedItem != null)
{
Mens_Leaders SelectedPlayer = mensRankings.SelectedItem as Mens_Leaders;
_SelectedMensPlayer = (sender as ListBox).SelectedItem;
NavigationService.Navigate(new Uri("/Pages/PlayerProfile.xaml", UriKind.Relative));
FrameworkElement root2 = Application.Current.RootVisual as FrameworkElement;
root2.DataContext = _SelectedMensPlayer;
}
}
Page where second list loads
private void LoadPlayerList()
{
Service1Client ServiceReference1Client = new Service1Client();
ServiceReference1Client.GetTournamentListCompleted += new EventHandler<GetTournamentListCompletedEventArgs>(ServiceReference1Client_GetTournamentListCompleted);
ServiceReference1Client.GetTournamentListAsync();
FrameworkElement root2 = Application.Current.RootVisual as FrameworkElement;
var currentPlayer = root2.DataContext as ATP_Tennis_App.TennisService.Mens_Leaders;
_SelectedPlayer = currentPlayer;
_selectedPlayerTournamentsWon = currentPlayer.Name;
}
void ServiceReference1Client_GetTournamentListCompleted(object sender, GetTournamentListCompletedEventArgs e)
{
if (e.Error != null) { return; }
ObservableCollection<Tournaments> tournamentList = e.Result;
viewSource = new CollectionViewSource();
viewSource.Filter += TournamentWin_Filter;
viewSource.Source = tournamentList;
this.listBox1.ItemsSource = viewSource.View;
}
void TournamentWin_Filter(object sender, FilterEventArgs e)
{
if (e.Item != null)
e.Accepted = ((Tournaments)e.Item).Prev_Male_Winner.Contains(_selectedPlayerTournamentsWon);
}
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (listBox1.SelectedItem != null)
{
Tournaments selectedTournament1 = listBox1.SelectedItem as Tournaments;
_SelectedItemTournament = (sender as ListBox).SelectedItem;
NavigationService.Navigate(new Uri("/Pages/TournamentDetailsPanorama.xaml", UriKind.Relative));
FrameworkElement root3 = Application.Current.RootVisual as FrameworkElement;
root3.DataContext = _SelectedItemTournament;
}
else
{
return;
}
}
On the listbox, set the IsSynchroziedWithCurrentItem to False. This fixed the issue.
I know this is a dirty solution, but you can set listBox1.SelectedIndex = -1

How to return the selected tab item's control in the container

I had a old windows form WebBrowser and I had:
public WebBrowser getCurrentWebBrowser()
{
return (WebBrowser)TabControl.SelectedTab.Controls[0];
}
I want to know how to do the same thing in WPF.
Thanks for any help.
You can hook the TabContols' SelectionChanged event and then have code like this in the callback...
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
HeaderedContentControl hcc = e.AddedItems[0] as HeaderedContentControl;
if (hcc != null)
{
WebBrowser webBrowser = hcc.Content as WebBrowser;
if (webBrowser != null)
{
// do something...
}
}
}
If you have a WebBrowser in more than one tab, you can name the TabItems and adapt your callback thusly...
<TabItem Header="Browser" Name="MyBrowserTabItem">
<WebBrowser/>
</TabItem>
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
HeaderedContentControl hcc = e.AddedItems[0] as HeaderedContentControl;
if (hcc != null && hcc.Name == "MyBrowserTabItem")
{
WebBrowser webBrowser = hcc.Content as WebBrowser;
if (webBrowser != null)
{
// do something...
}
}
}
The 'AddedItems' property of the SelectionChangedEventArgs is a list of objects that have been selected since the last time the event was fired. For a TabControl, AddedItems should normally have a length of 1, and the single element should be an instance of HeaderedContentControl (i.e., the TabItem).

Categories

Resources