Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually there is no "right" way, there is only what works best, and what is most appropriate given the context.</p> <p>Your example looks fine, it seems, so far at least, the only issue you have, is that you will not have the selected option in sync with what the user chose/didn't choose, the last time they used the software.</p> <p>This requires only two small pieces of your code to be in two particular places.</p> <ul> <li>Austin pointed out one of them already: Save your settings. This you should do right after your if/else in your method: <code>MenuItem_Click</code>. Just make sure the method does not exit somehow before the call to <code>Settings.Sav</code>e is made... a <code>try</code>/<code>catch</code> with a graceful way of ensuring a consistent settings state would be prudent.</li> <li>The other is at the "time" you yourself mentioned: Initialization, or startup of the app. Somewhere in your app, before the initial loading is completed, you must access the setting you created (toggleThis) and use it to set the initial state of your menu item.</li> </ul> <p>The best way to facilitate this, is to have a private method, which is responsible for both changing what icon is showing on the menu item, as well as storing the most recent state in the settings of the app. A method called <code>Toggle()</code> perhaps, which you call within your <code>MenuItem_Click</code> method. You need to give the menu item in question and ID though, that can be used to access the menu item in your code-behind though. As well, this code example assumes you have your icons stored in settings as well, although the icons can be coming from wherever, as long as you can reference them.</p> <p>So your code could be something like this, although not exactly this:</p> <pre><code>public MainWindow() { InitializeComponent(); this.SetToggleIcon(Properties.Settings.Default.toggleThis); } private void Toggle() { this.StoreToggleState(!Properties.Settings.Default.toggleThis); this.SetToggleIcon(Properties.Settings.Default.toggleThis); } private void SetToggleIcon(bool state) { this.menuItem_ToggleSettings.Icon = (Properties.Settings.Default.toggleThis) ? Properties.Settings.Default.IconTrue : Properties.Settings.Default.IconFalse; } private void StoreToggleState(bool state) { Properties.Settings.Default.toggleThis = state; Properties.Settings.Default.Save(); } private void MenuItem_Click(object sender, RoutedEventArgs e) { this.Toggle(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload