I have a multi-select ListBox and I try to deselect all items like this:
private void _SelectionChanged(object sender, SelectionChangedEventArgs e) {
foreach(var i in e.AddedItems) {
// do whatever - works ok
}
//then clear all selected - doesn't work
((ListBox)(e.OriginalSource)).SelectedItems.Clear();
}
I have tried (e.OriginalSource as Listbox).SelectedItems.Clear() as well, but it just throws an error.
Any advice would be appreciated, thanks.
EDIT:
After many tries this works:
((ListBox)sender).SelectedItem = null;
regardless of the fact that it is multi- not single- select ListBox!
The following is what you were actually looking for:
((ListBox)sender).SelectedItems.Clear();
The reason calling .SelectedItem = null works is just the behavior of the ListBox. Both end up doing the same thing. The more "correct" one is the line I gave you.
Related
I am trying to develop an app using Xamarin.Forms. At a certain point I am trying to have multiple switches that are grouped. This is to say that when one switch is toggled, every other switch needs to be untoggled and, at the same time, there needs to be at least one switch always toggled. This is to say that tapping on a switch that is already toggled should not change anything.T
Now my problem is that Xamarin.forms Toggled event for switches can be fired from the UI, but is also fired programmatically. I thought I had found a way around this problem, but still by doing:
-If the switch was turned on, turn off all others and do application stuff.
-else if a switch was turned off, check if there are any others that are on. If not, turn the switch back on. If yes, do nothing.
A sample code for two switches could be:
private void OnFirstToggled(object sender, EventArgs args)
{
if(FirstSwitch.isToggled)
{
//Application stuff.
SecondSwitch.isToggled = false;
}
else if (!SecondSwitch.isToggled)
{
FirstSwitch.isToggled = true;
}
}
private void OnSecondToggled(object sender, EventArgs args)
{
if(SecondSwitch.isToggled)
{
//Application stuff.
FirstSwitch.isToggled = false;
}
else if (!FirstSwitch.isToggled)
{
SecondSwitch.isToggled = true;
}
}
This solution results in an infinite loop when an already toggled switch is tapped. In fact, the isToggled property of the switch alternates between true and false infinitely. However when debugging the other event never seems to be fired (or at least my debugger does not see it). This is why I don't understand where the isToggled property is changed after that first tap.
I know this is probably a very simple issue, but I cannot seem to find the solution somewhere online. Can anyone see the problem or recommend a better, common way to implement this?
I write a simple solution to you to always keep one Switch open from a Switch group.
Let's first add three switch for test, make sure these Switch will fire the same event of Toggled:
<StackLayout>
<!-- Place new controls here -->
<Switch Toggled="Switch_Toggled" x:Name="FirstSwitch"/>
<Switch Toggled="Switch_Toggled" x:Name="SecondSwitch"/>
<Switch Toggled="Switch_Toggled" x:Name="ThirdSwitch"/>
</StackLayout>
In the code behind, I add those Switches into a list, and loop them in Switch_Toggled event to open/close the Switches:
public partial class MainPage : ContentPage
{
List<Switch> switchList;// To store all your Switches
bool isLooping; //To make sure the Switch_Toggled metod not fired a second time during one toogle event
public MainPage()
{
InitializeComponent();
switchList = new List<Switch>();
switchList.Add(FirstSwitch);
switchList.Add(SecondSwitch);
switchList.Add(ThirdSwitch);
isLooping = false;
}
private void Switch_Toggled(object sender, ToggledEventArgs e)
{
//To make sure the Switch_Toggled metod not fired a second time during one toogle event
if (isLooping == true)
{
return;
}
isLooping = true;
Switch clickSwitch = sender as Switch;
clickSwitch.IsToggled = true;
foreach (var tempSwitch in switchList)
{
if (tempSwitch != clickSwitch)
{
if (tempSwitch.IsToggled == true)
{
tempSwitch.IsToggled = false;
}
}
}
isLooping = false;
}
}
You can try this solution and feel free to ask me any question if you don't understand.
Your problem are the two else blocks. Take in account that you're toggling it on anyway.
I quickly made a Windows Forms project which loads a GUI of different textboxes with float values. Some of them do have already a value initialized. All textboxes have to be updated after one of them is changed.
public Form1()
{
InitializeComponent();
initializeValues();
calculateValues();
}
public void initializeValues()
{
//textboxes are filled/initialized with default float values
}
public void calculateValues()
{
//here all textboxes are new calculated and updated
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
calculateValues();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
calculateValues();
}
Problem:
When I execute this project, it throws me a StackOverflowException which is unhandeled (infinite loop or infinite recursion). I think it's because during the calculateValues() method the text of the textBoxes will be changed and then the Eventhandlers are activated. That's the infinite loop :-(
How I have to change my code construct above to avoid this?
Thanks.
You should not using and calling "initializeValues();" (the cause of the infinite loop).
A first solution could be to put the init value of a TextBox in InitializeComponent :
MyTextBox.Text = myInitValue;
I solved the problem by changing the Event to "KeyPress". In this case, the Event is not activated by the method itself. No more infinite loops. Setting breakpoints and step through helped me to understand "the flow". Thanks CodeCaster.
Please guide me how to prevent new rows added automatically on DevExpress.XtraGrid.GridControl
I want to control when new rows is added, in my case i'm using a keydown event (CTRL + I ) for this task. But the grid keep adding new rows automatically if i move the focus (cursor pointer) to the area right below to the last row and click.
The GridControl.MainView is a BandedGridView, which contains the datasource.
You can use BandedGridView.OptionsView.NewItemRowPosition property. You can set its value to NewItemRowPosition.None to hide new item row.
Another way is to handle BandedGridView.ShownEditor event. Inside of this you can check if BandedGridView.FocusedRowHandle property is equals to GridControl.NewItemRowHandle and cancel editor activation.
Here is example:
private void bandedGridView1_ShowingEditor(object sender, CancelEventArgs e)
{
if (bandedGridView1.FocusedRowHandle == GridControl.NewItemRowHandle)
{
// Do here additional checks if you need. After your checks set e.Cancel to true.
e.Cancel = true;
}
}
You can handle the ValidateRow event. If you set e.Valid = false you wont add a new row. So check if your object is empty or invalid and just if the needed values are typed you give the row free.
private void grvMyView_ValidateRow(object sender, ValidateRowEventArgs e)
{
if (grvMyView.IsNewItemRow(e.RowHandle))
{
MyObject obj = grvMyView.GetRow(e.RowHandle) as MyObject;
e.Valid = obj.IsValid();
}
}
As of version 15 you can simply set your TableView's NewItemRowPosition to NewItemRowPosition.None. Be sure to call CommitEditing() on your TableView first.
I have a ListBox populated with data coming from XML.
Fine so far, the problem is that I get some errors when I try to tombstone it.
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
State["listbox1"] = listBox1.ItemsSource;
}
Then:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (State.ContainsKey("listbox1"))
{
listBox1.ItemsSource = (IEnumerable)State["listbox1"];
}
}
When I hit the start button I already get an error. The App.xaml.cs opens and the line below becomes yellow
System.Diagnostics.Debugger.Break();
I've also used tombstoning helper but it did not return the items in my listbox.
What is the listbox bound to? And what error are you seeing?
If it's a DataServiceCollection, you may have tracking turned on & you cannot put it flatly in Isolated Storage or State dictionaries. Should be fine if using ObservableCollection.
Thanks!
I'm trying to make an ApplicationBarMenuItem that, when clicked, switches my bing map between RoadMode and AerialMode. my pseudocode looks something like this:
private void changeMap_Click(object sender, EventArgs e)
{
if(map1.Mode == RoadMode)
map1.Mode = new Microsoft.Phone.Controls.Maps.AerialMode();
else
map1.Mode = new Microsoft.Phone.Controls.Maps.RoadMode();
}
However, it says I cannot use RoadMode, which is a 'type', like a variable. Does anyone have a way around this?
Because RoadMode is a class and you are trying to compare class to an object.
Try (edit)
if(map1.Mode is RoadMode)