locked
Displaying a WPF Window from ViewModel in C# RRS feed

  • Question

  • I have a C#/WPF app based on MVVM pattern. The app has a window with a tool bar. On clicking a button on this toolbar, I need to show a Window like a settings window. The toolbar button is bound to a command that should load the settings window. I have designed the  WPF settings window. The user will be able to select some settings(preferences) from this second window. This I understand is not like page navigation but loading the window. When the settings window is displayed the main window will be in the background. How I can I do this from the ViewModel of the main window. Below is some existing code:

    <ToolBarTray Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Top" Height="auto">
            <ToolBar Name="MyToolbar" BorderThickness="1"    >
                    <Button   x:Name="SettingsButton"  Command="{Binding LoadSettingsCommand}">
                        <Image Source="options.ico"  ></Image>
                </Button>
            </ToolBar>
    </ToolBarTray>
    public class MyViewModel : INotifyPropertyChanged
        {
    
            private ICommand _loadSettingsCmd;
            
    
    	public ICommand LoadSettingsCommand
            {
                get
                {
                    if (null == _loadSettingsCmd) { _loadSettingsCmd = new MyRelayComman(param => this.LoadToDoSettings(), null); }
                    return _loadSettingsCmd;
                }
                set
                {
                    _loadSettingsCmd = value;
                }
            }
    
    	public MyViewModel()
            {
    		  _loadSettingsCmd = new MyRelayCommand(param => this.LoadToDoSettings(), null);
    	}
    	private void LoadToDoSettings()
            {
                // Load Settingg window
            }
    }
    I am able to  reach the LoadSettings() method when clicking the toolbar button. How can I display the window. I saw some examples that load usercontrol in similar scenario. Can we load normal WPF window like a modal window as needed in this example?

    • Moved by CoolDadTx Tuesday, July 28, 2020 1:54 PM WPF related
    Tuesday, July 28, 2020 1:47 PM

All replies

  • Please use the following WPF forum

    https://docs.microsoft.com/en-us/answers/topics/wpf.html


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, July 28, 2020 1:51 PM
  • Thank you
    Wednesday, July 29, 2020 6:55 AM
  • I logged onto the link. I understand that is Microsoft Q And A forum. But  when I post the question I am getting the below error:

    You don't have permission to access "http://docs.microsoft.com/answers/questions/ask.html" on this server.

    I have posted in this forum before.

    Can someone please help?

    Wednesday, July 29, 2020 2:57 PM
  • HI , I still am not able to post to the above forum. Can someone please move my question to the appropriate forum?
    Monday, August 10, 2020 5:29 AM
  • Any updates?
    Tuesday, August 11, 2020 7:31 PM
  • Might be the content. Try posting a simple "testtesttest" for subject and body. (need at least 10 chars in both subject and body)

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Tuesday, August 11, 2020 8:16 PM