I'm using a navigationview control in a UWP app, the thing is when I click the back button the focused element doesn't change to the item displayed in the contentframe. For example the clicked elements were camara, store, musica, then back button twice to display camara in the contentframe element, but musica still has the focus (blue rectangle)
private void FrameNavigated( object sender, NavigationEventArgs e )
{
var currentView = SystemNavigationManager.GetForCurrentView();
if ( ContentFrame.CanGoBack )
{
currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
}
else
{
currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
}
}
private void OnBackRequested( object sender, BackRequestedEventArgs e )
{
if ( ContentFrame.CanGoBack )
{
e.Handled = true;
PageStackEntry pageStackEntry = ContentFrame.BackStack.LastOrDefault();
ContentFrame.GoBack();
if ( pageStackEntry != null )
{
string nombre = pageStackEntry.SourcePageType.Name;
MenuItem item = subItemsMenu.FirstOrDefault(nom => nom.NombrePantalla.Equals(nombre));
navView.SelectedItem = item;
navView.Header = item.Encabezado;
}
}
}
Solved, using
NavigationViewExtensions.SetSelectedIndex(NavigationView, index); from Microsoft.Toolkit.Uwp.UI.Extensions 4.0.0, since 5.0.0 version is deprecated
I didn't see any problem in your code. Several ways may help to troubleshooting:
Debug and make sure MenuItem item is not null.
When back button is clicked, try select home or camera directly by your code
private void OnBackRequested( object sender, BackRequestedEventArgs e )
{
string nombre = "Camera";
MenuItem item = subItemsMenu.FirstOrDefault(nom => nom.NombrePantalla.Equals(nombre));
navView.SelectedItem = item;
}
Add SelectionChanged="nav_SelectionChanged" to the XAML and then check SelectionChanged
Post a simple app which can repro your problem
Related
I customize the right click menu thanks to this :
lineGraphControl1.ContextMenuBuilder += new ZedGraphControl.ContextMenuBuilderEventHandler(MyContextMenuBuilder);
And
private void MyContextMenuBuilder(ZedGraphControl control, ContextMenuStrip menuStrip, Point mousePt, ZedGraphControl.ContextMenuObjectState objState)
{
// create a new menu item
ToolStripMenuItem item = new ToolStripMenuItem();
// This is the user-defined Tag so you can find this menu item later if necessary
item.Name = "simple_cursor";
// This is the text that will show up in the menu
item.Text = "Simple Cursor";
item.CheckOnClick = true;
// Add a handler that will respond when that menu item is selected
item.Click += new System.EventHandler(DisplaySimpleCursor);
// Add the menu item to the menu
menuStrip.Items.Add(item);
}
But the menu Simple Cursor won't check when clicked. I try to force the sender in the function DisplaySimpleCursor(), it doesn't work.
When I debug my app, I see that in DisplaySimpleCursor(), the sender's property Checked is set to true.
What am I missing ?
As the menu is build on the heat, the checkOnClick means nothing since the object is destroyed (I guess) everytime the menu is hidden.
The solution was to set the property :
// showOneCursor is a bool describing my need and toggled on click
item.Checked = showOneCursor;
Try this.
private bool check;
public bool Check
{
get { return check; }
set { check= value; }
}
private void MyContextMenuBuilder(ZedGraphControl control, ContextMenuStrip menuStrip, Point mousePt, ZedGraphControl.ContextMenuObjectState objState)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Name = "simple_cursor";
item.Text = "Simple Cursor";
item.CheckOnClick = true;
item.Checked = Check; //add this
item.Click += new System.EventHandler(DisplaySimpleCursor);
menuStrip.Items.Add(item);
}
private void DisplaySimpleCursor(object sender, EventArgs e)
{
Check = false==Check;
}
I am new to WPF, and I want to do something when a user switches between my tabcontrol items.
As expected, I had the issue of firing the selectionchanged event multiple times, then I read this post:
In C# WPF, why is my TabControl's SelectionChanged event firing too often?,
and I don't like the first solution which requires too many extra code for handling event for each selectors in the application. Hence, I tried the solution in this post:
TabControl's SelectionChanged event issue,
but I got a new issue that I couldn't find any related post in stackoverflow.
The problem I have is that the following code doesn't return true:
if (e.Source is TabControl){ // do something }
neither this one:
if (e.Source is TabItem) {// do something}
When I hover on the e.Source in debug mode, it shows as
{System.Windows.Controls.TabControl Items.Count:5}
and if I tried to view it in WPF Tree Visualizer, it tells me that it is the TabControl that I expected for.
So my question is, why it doesn't return true since it is a TabControl?
Here is my code for SelectionChanged:
void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.Source is TabControl)
{
if (item1.IsSelected)
{
myllist1.DataContext = getList1();
}
else if (item2.IsSelected)
{
mylist2.DataContext = getlist2();
}
else if (item3.IsSelected)
{
mylist3.DataContext = getlist3();
}
else if (item4.IsSelected)
{
mylist4.DataContext = getlist4();
}
}
}
You have to convert e.source from an Object to a FrameworkElement, and then compare the types.
if (((FrameworkElement)e.Source).GetType()== typeof(System.Windows.Controls.TabControl))
{
if (item1.IsSelected)
{
myllist1.DataContext = getList1();
}
else if (item2.IsSelected)
{
mylist2.DataContext = getlist2();
}
else if (item3.IsSelected)
{
mylist3.DataContext = getlist3();
}
else if (item4.IsSelected)
{
mylist4.DataContext = getlist4();
}
}
I have a UserControl which I am loading into a div which is inside an UpdatePanel. Here is my code for loading it:
controls.IDLControl IdlControl = LoadControl(#"~/controls/IDLControl.ascx") as controls.IDLControl;
IdlControl.ClientIDMode = ClientIDMode.Static;
IdlControl.ID = "IDLControl";
spGroup.Controls.Clear();
spGroup.Controls.Add(IdlControl);
And here is my code for trying to retrieve an instance of it:
controls.IDLControl IdlControl = RecursiveFindControl(this, "IDLControl") as controls.IDLControl;
private Control RecursiveFindControl(Control targetControl, string findControlId) {
if (targetControl.HasControls()) {
foreach (Control childControl in targetControl.Controls) {
if (childControl.ID == findControlId) {
return childControl;
}
RecursiveFindControl(childControl, findControlId);
}
}
return null;
}
But, all I get is null. I need help on figuring this out.
AFAIK, I need to re-add the control to the page on pre-init but it is one of the controls that can be added depending on which option is selected from a drop down list (which also is filled dynamically). I am stuck trying to figure out how to make this work.
You can try something like this to add your control back in the Page_Init based on the option selected in your DropDownList.
protected void Page_Init(Object sender, EventArgs e)
{
if (IsPostBack)
{
if (drpYourDropDown.Items.Count > 0 && drpYourDropDown.SelectedItem.Text == "yourOption")
{
AddIDLControl();
}
}
}
private void AddIDLControl()
{
controls.IDLControl IdlControl = LoadControl(#"~/controls/IDLControl.ascx") as controls.IDLControl;
IdlControl.ClientIDMode = ClientIDMode.Static;
IdlControl.ID = "IDLControl";
spGroup.Controls.Clear();
spGroup.Controls.Add(IdlControl);
}
Consider a SilverLight project that has 31 hyperlinkbuttons. Those represent the days of the month. I'm using this code to highlight the hyperlinkbutton that respresent today's day.
var daynumberHyperButton = this.FindName("Day" + DateTime.Today.Day) as HyperlinkButton;
//Highlighting the day of the month
if (daynumberHyperButton != null)
{
daynumberHyperButton.Background = new SolidColorBrush(Colors.Gray);
}
Then if I click on this highlighted hyperlinkbutton, it will open a childwindow to write some report.
private void dayHyperLink_Click(object sender, RoutedEventArgs e)
{
//This will initite and show the report window
ReportWindow rapport = new ReportWindow();
rapport.Closed += new EventHandler(rapport_Closed);
rapport.Show();
}
When I close the childwindows by clicking the OK button, it changes the color of the hyperlinkbutton that was highlighted (todays day) because I'm using this code to do that:-
private void rapport_Closed(object sender, EventArgs e)
{
ReportWindow rapport = (ReportWindow)sender;
var daynumberHyperButton = this.FindName("Day" + DateTime.Today.Day) as HyperlinkButton;
if (rapport.UsersValue == "Röd" && rapport.DialogResult==true)
{
daynumberHyperButton.Background = new SolidColorBrush(Colors.Red);
}
else if (rapport.UsersValue == "Gul")
{
daynumberHyperButton.Background = new SolidColorBrush(Colors.Yellow);
}
else
{
daynumberHyperButton.Background = new SolidColorBrush(Colors.Green);
}
}
But if I click on any other hyperlinkbutton that is not highlighted, it still only change the color of the highlighted hyperlinkbutton. I know this because my rapport_Closed event has:
var daynumberHyperButton = this.FindName("Day" + DateTime.Today.Day) as HyperlinkButton;
How can I change the above code, which is part of my rapport_Closed event, so that it changes the color of the event firing (the one that opens the childwindow) hyperlinkbutton, no matter which hyperlinkbuttonis the one that fires the event?
Ok now i can say i've done it. Here is what i did if anyone have a similar problem.
In may Home.xaml.cs, i added a public property like this:-
public HyperlinkButton dayHyperLink { get; set; }
To the Click event i added this code:-
dayHyperLink = (HyperlinkButton)sender;
To the rapport_Closing event i changed the if statment to the code below:-
if (rapport.UsersValue == "Röd" && rapport.DialogResult == true)
{
dayHyperLink.Background = new SolidColorBrush(Colors.Red);
}
This made me feel happy ;)
I have this code:
public static void AddDefaultTextFromTag(params TextBox[] textBoxes)
{
foreach (TextBox oTextBox in textBoxes)
{
bool isPasswordChar = oTextBox.UseSystemPasswordChar;
oTextBox.Enter += (sndr, evnt) =>
{
if (((TextBox)sndr).Text == ((TextBox)sndr).Tag.ToString())
{
((TextBox)sndr).Text = "";
((TextBox)sndr).UseSystemPasswordChar = isPasswordChar;
((TextBox)sndr).ForeColor = SystemColors.WindowText;
}
};
oTextBox.Leave += (sndr, evnt) =>
{
if (((TextBox)sndr).Text.Trim().Count() == 0)
{
((TextBox)sndr).UseSystemPasswordChar = false;
((TextBox)sndr).CharacterCasing = CharacterCasing.Normal;
((TextBox)sndr).Text = ((TextBox)sndr).Tag.ToString();
((TextBox)sndr).ForeColor = SystemColors.GrayText;
}
};
if (oTextBox.Text.Trim().Count() == 0)
{
oTextBox.UseSystemPasswordChar = false;
oTextBox.CharacterCasing = CharacterCasing.Normal;
oTextBox.Text = oTextBox.Tag.ToString();
oTextBox.ForeColor = SystemColors.GrayText;
}
}
}
But when the TextBox.UseSystemPasswordChar I input in this method's parameter is true and it's TextBox.Text property is empty, the TextBox can't leave using a Tab button on the keyboard, only a MouseClick can be used to lose the focus of that TextBox.
Why is this happening?
My code is in C#, framework 4, build in VS2010 Pro, project is in WinForms.
I use a TextBox from the VS ToolBox.
Please help. Thanks in advance.
The reason you can't leave the textbox is because you are changing the CharacterCasing property in the textbox.
Not sure why it works like this, but it has happened to me before, what I ended up doing was capture the keypress event, and if it was a letter I'd switch it to it's uppercase value. It's not optimal, but it works
I did something similar to this (writing it from the top of my head, but it should work):
void YourTextbox_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsLetter(e.KeyChar))
{
if (this.CharacterCasing == CharacterCasing.Upper && char.IsLower(e.KeyChar))
{
this.Text = this.Text.Insert(this.SelectionStart, char.ToUpper(e.KeyChar) + string.Empty);
this.SelectionStart++;
e.Handled = true;
}
else if (this.CharacterCasing == System.Windows.Forms.CharacterCasing.Lower && char.IsUpper(e.KeyChar))
{
this.Text = this.Text.Insert(this.SelectionStart, char.ToLower(e.KeyChar) + string.Empty);
this.SelectionStart++;
e.Handled = true;
}
}
}
You also should use the new keyword to "override" (I know that's not the right term here) the Character casing, so it doesn't do it's own thing
public new CharacterCasing CharacterCasing { get; set; }
The code basically checks if the pressed key is a letter, then, if it's marked as Upper, and the char is lower, replaces it with it's upper version (in the position of the cursor) then moves the cursor to the next part, and Viceversa (toLower)
NOTE:
This code will have may (should) have some trouble if the user has more than one character selected (SelectionLenght > 0), if you want to keep the normal Textbox functionality, you should delete all the selected characters
So I set up a WinForms app, drew two textboxes, set one to UseSystemPasswordChar=true then set it up like so:
private void Form1_Load(object sender, EventArgs e)
{
textBox2.Tag = "test2";
textBox1.Tag = "test1";
TextBox[] tb = { textBox1, textBox2 };
AddDefaultTextFromTag(tb);
}
Your function works fine and I have no problems tabbing through the controls on the form no matter what the textboxes contain. (added a button also that does nothing for tabbing test) so... no repro unless my test setup is not valid
What I found in the answer of this post was the solution for me. Instead of setting UseSystemPasswordChar to true and then to false, you can set PasswordChar to '●' and then to '\0' to have normal text. You should not set the UseSystemPasswordChar because it has precedence over PasswordChar.