Zoya Sheikh
Zoya Sheikh

Reputation: 939

How to save image source in WPF?

I have created WPF Application in which when user will select an image and save it, it will be applied to all buttons, navigation bar items etc. I wrote code to save image path in Settings.settings File. As i select image it save this to database but not apply to navigation bar items or buttons source of image until i restart my application. Here my code is:

    public partial class MainWindow : DXWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            Refreshicon();
        }

        public void Refreshicon()
        {
            BitmapImage bi = new BitmapImage(new Uri(ApplicationSettings.Default.ImageName));  //Image From Settings File!
            MessageBox.Show("Image Path" + bi.ToString());
            navBarGroup1.ImageSource = bi;
            navBarGroup2.ImageSource = bi;
            navBarItem1.ImageSource = bi;
            navBarItem2.ImageSource = bi;
        }

How can i apply user defined image path tonavigation bar items or buttons without restarting my application?

Edit //This below code is to save image and to call Refreshicon() function

 private void Button_Click_SaveImage(object sender, RoutedEventArgs e)
        {
        string imagepath = ApplicationSettings.Default.ImageName;
        ApplicationSettings.Default.SetImage(imageEdit1.ImagePath);
        MainWindow a = null;
        if (a == null)
            {
            a=new MainWindow();
            a.Refreshicon();

            }

        }

Upvotes: 0

Views: 2457

Answers (1)

Tony Vitabile
Tony Vitabile

Reputation: 8594

You'll have to call your Refreshicon() method in whatever code is writing the image path to the Settings.settings file.

Edit:

If Button_Click_SaveImage is in a window other than the MainWindow, then you need to add a reference to the original MainWindow instance to the child window class and call its Refreshicon() method. Something like this:

In the child window class, let's call it DialogWindow:

public class DialogWindow : Window {
    // All your code here . . .

    public MainWindow Parent { get; set; }

    private void Button_Click_SaveImage( object sender, RoutedEventArgs e ) {
        // Your code up to the MainWindow a line goes here
        Parent.Refreshicon();
    }
}

Then, in MainWindow, when you instantiate the child window for display:

DialogWindow childWindow = new DialogWindow();
childWindow.Parent = this;

Upvotes: 2

Related Questions