Windows 8 - cache paged button visual state not changing on navigate back - c#

I have a small (but annoying) problem.
You can quickly replicate it by doing the following:
New Project > Windows Store > Blank App (XAML)
Add a button to the grid. This also works with the default style. (note: TextButtonStyle is defined in SimpleStyles.xaml)
<Button Click="Click" Style="{StaticResource TextButtonStyle}" Content="Page 2"/>
Add the function to the code behind file:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof (Page2));
}
Next create another page, add the button, and navigate back to the MainPage in the Click event.
Next set on both pages add NavigationCacheMode="Enabled". For convenience set one of the buttons to be Left aligned and the other one Right.
Run the app. Move the mouse over the button. The state changes to reflect this. Click the button. Again the color changes. On the second page, do the same. On returning to the first page, the button is still in it's "PointerOver" visual state, since there was no PointerExited event called on the button.
How can I fix this? VisualStateMananger.GoToState() doesn't work.

We encountered a similar issue. We noticed that the state 'resets' when you hide the control.
We solved it, the dirty way:
void GridView_ItemClick(object sender, ItemClickEventArgs e)
{
var ctrl = this.ItemContainerGenerator.ContainerFromItem(e.ClickedItem);
((Control)ctrl).Visibility = Visibility.Collapsed;
((Control)ctrl).Visibility = Visibility.Visible;
}
Maybe you could try to do the following (didn't test this):
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
((Control)sender).Visibility = Visibility.Collapsed;
((Control)sender).Visibility = Visibility.Visible;
Frame.Navigate(typeof (Page2));
}

Related

How can I disable (grey out) specific tabpage(s) of a tab control programatically in my C# form app?

I can't believe that disabling specific tabpages (visible but greyed out that means not clickable) is not possible in Visual Studio C# Form app. I have found just a workaround that I couldn't bring to work.(See link below)
I wanted to ask this question again, because perhaps there is another solution/workaround in the meantime.
How to highlight/grey-out Tabcontrol Tabpage
For just making the tab nonclickable (what is also OK for me) I have used this code, but not working
private void tabControl1_Selecting(object sender,TabControlCancelEventArgs e)
{
if (!e.TabPage.Enabled)
{
e.Cancel = true;
}
}
For example if you want to disable tabPage2 use this code in form load event. It will not show in intellisense but you can type.
tabPage2.Enabled = false;
Then use tab control selecting event like this
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
if (e.TabPageIndex < 0) return;
e.Cancel = !e.TabPage.Enabled;
}

WPF Press any button to continue to next page

Got a simple 3 page program, but would like to make a welcome screen that users have to press a button to move to the next page (and start the workflow).
Users will mostly be using a barcode scanner (captures string & hits enter), so I'm trying to avoid buttons that need to be clicked with a mouse.
The setup is basically one MainWindow with a DockPanel in it with a Frame in it.
Pages get loaded to the frame on events(the enter being hit on a textbox). Pages have Grids in them with the standard controls.
I've got the frame in the window navigating to the welcome page, but can't seem to capture a keypress.
What would set I the event on? The Window, the frame, the dockpanel, the page, the grid?
Also, is there a way to specify "any key or click" event, maybe listen for any input action?
I've tried experimenting and looking at documentation but I can't seem to get it.
I created an empty WPF app with a frame and was able to catch a keypress event. Once the app starts I focus the frame. You can do the same once your page loads or when your workflow starts over.
<Frame Loaded="FrameworkElement_OnLoaded" KeyUp="UIElement_OnKeyUp">
<Frame.Content>
<Page>
<Label Content="Hello"></Label>
</Page>
</Frame.Content>
</Frame>
private void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e)
{
((Frame)sender).Focus();
}
private void UIElement_OnKeyUp(object sender, KeyEventArgs e)
{
MessageBox.Show("Key pressed");
}
Add this to your designer:
this.myForm.KeyDown += new System.Windows.Forms.KeyEventHandler(this.myForm_KeyDown);
And add this to your Form's code
private void myForm_KeyDown(object sender, KeyPressEventArgs e)
{
if(KeysDown().Any())
{
currentForm.Hide();
nextForm,Show();
}
}

Is there a RichTextButton in C#? [duplicate]

I have done a bit of research but cannot seem to find what I am looking for.
What I want to do is make a "custom" button in a windows form. This would basically just be a matter of changing the default "grey" background to a custom image. A custom image would also be used for when hovering over and clicking the button.
This is not just a matter of changing the background image as the image I want to use has rounded edges with a transparent background and I want custom image for hovering / clicked. I want everything else about the button to behave in the same manner as a normal button.
Is this possible?
It is called owner-drawn button
refer to:
Mick Dohertys' .net Tips and Tricks - Tips / Button
GlowButton - A Glowing Button Control
A shiny orb button in GDI+
The solution I found was to set the FlatStyle of the button to Flat and set all the borders to 0. I then had a problem with the focus of the button (it displayed a little border). To solve this I followed this tutorial:
http://dotnetstep.blogspot.com/2009/06/remove-focus-rectangle-from-button.html
With this in place all I had to do was add events to the button so that the image was changed when a certain action was carried out on it:
private void button1_MouseLeave(object sender, EventArgs e)
{
this.button1.Image = Properties.Resources._default;
}
private void button1_MouseEnter(object sender, EventArgs e)
{
this.button1.Image = Properties.Resources._hover;
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
this.button1.Image = Properties.Resources._clicked;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
this.button1.Image = Properties.Resources._default;
}
Hope this will help someone else!

Win Phone 8 : Navigating Only One Time

I'm on windows phone 8 project. Doing it with xaml in visual studio.
I have 4 pivot items in this project.
I want to navigate the home page (my first pivot item) with
menu on bottom of phone;
<shell:ApplicationBarIconButton IconUri="Images/appbar_home.png" Text="Home" Click="ApplicationBarIconButton_Click_2"/>
and on click event i wrote this;
private void ApplicationBarIconButton_Click_2(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/MainPage.xaml?PivotMain.SelectedIndex = 0", UriKind.Relative));
}
When i run the program, it works nice, when i go other pivot items, and click that home button, it goes, but if i walk around again and click button, it doesnt work. Why ?
I need to solve this.
Thanks for the answers.
You are having this problem because you are doing it wrong.
NavigationService.Navigate is meant to navigate between XAML Files, not reload them. The phone doesn't like to reload the same page over and over.
What you want to do instead is set the current selected index to the first page.
Reference:http://msdn.microsoft.com/en-us/library/windowsphone/develop/microsoft.phone.controls.pivot(v=vs.105).aspx
It will look something like this
private void ApplicationBarIconButton_Click_2(object sender, EventArgs e)
{
PivotControlName.SelectedIndex = 0;
}

ToolStripButton "Reset Appearance" / "Fire Leave Event"

I have a ToolStripButton that performs an action. The user clicks the button and the button is disabled to prevent the action being performed twice. After the action is complete the button is re-enabled. It all works perfectly...except:
Because the button is disabled it does not fire the "MouseLeave" event and as a result the appearance of the button is not updated. To be absolutely clear, when the mouse enters a ToolStripButton the button is highlighted in orange (by default) with a black box around it. This highlight is not being removed when I re-enable the button. The mouse cursor is, by this time, long gone from the control. Mousing over the button naturally fixes the button by redrawing it.
What I would like to do would be some method on the ToolStripButton that "resets" its appearance. Such a method may even exist on the ToolStrip, but despite searching I have been unable to find anything like this.
As an alternative I could fire the "Mouse Leave" event on the button directly. As far as I know there is no way to easily do this in C# .NET.
Any advice at this point in time would be most appreciated, naturally I don't want to tear up my application and replace the tool strip.
Update:
I reproduced your problem, trying figuring out!
I didn't get a better way other than reset the style in the click event
private void toolStripButton1_Click(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
toolStripButton1.Enabled = false;
}
private void toolStripButton1_MouseEnter(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.Red;
}
private void toolStripButton1_MouseLeave(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
}
Hope this helps!
Have you tried Control.Invalidate()?
from MSDN: Invalidates the entire surface of the control and causes the control to be redrawn.
I had the same problem. I "fixed" it by hiding and then showing back the ToolStripButton using the Visible property after the task was complete.
Before disabling ToolStrip or ToolStripItem:
private void RemoveHighlightFromToolStrip(ToolStrip toolStrip)
{
foreach (ToolStripItem item in toolStrip.Items)
{
if (item.Pressed || item.Selected)
{
item.Visible = false;
item.Visible = true;
}
}
}
also you can just hide and show entire ToolStrip, but this may affect other controls in your form (i.e. if you have some docked DataGridView it would be redrawn)

Categories

Resources