Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display C# Value in the WPF Front with Databiding

I’m trying to display if the application Docker Desktop is open or close, I have the good value in my c# when i log it but in the wpf the first value is display but it never get updated. I have look many tutorial but i can’t found why it doesn’t work with me.

Here is my XAML :

<Window x:Class="GrafanaLauncher.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GrafanaLauncher"
        mc:Ignorable="d"
        Title="MainWindow" ResizeMode="NoResize" Height="450" Width="800">
    <Window.DataContext>
        <local:DockerViewModel/>
    </Window.DataContext>
    <Grid>
        <Label Content="{Binding Docker.Status}" Margin="303,110,303,268"></Label>
        <Button Margin="258,192,427,192" Content="Start Grafana" Click="ButtonStartGrafana" Background="Green"></Button>
        <Button Margin="427,192,258,192" Content="Stop Grafana" Click="ButtonStopGrafana" Background="Red"></Button>
        <Button Style="{StaticResource OpenGrafanaTemplate}" Margin="340,293,340,101"  Width="120" Height="40" BorderBrush="#fff" Name="OpenGrafanaButton"  Click="OpenGrafana">
            <Underline>Open Grafana
            </Underline>
        </Button>
    </Grid>
</Window>

And here is my c# :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class DockerViewModel : ObservableObject
    {

        private DockerModel dockerModel;

        public event PropertyChangedEventHandler PropertyChanged;

        public DockerViewModel()
        {

            DockerUpdate();
            //Debug.WriteLine(Docker.Status);

            Task.Run(() =>
            {
                while (true)
                {

                    if (Docker == null)
                    {
                        Docker = new DockerModel("CHECKING");
                    }else
                    {
                        Debug.Write("FJKDHSGFD");
                    }

                    Thread.Sleep(1000);

                }
            });

        }

        public DockerModel Docker
        {
            get => dockerModel;
            set
            {
                if (dockerModel != value)
                {
                    dockerModel = value;
                    OnPropertyChanged(dockerModel.Status);
                }
            }
        }

        public void DockerUpdate()
        {

            Task.Run(() =>
            {
                while (true)
                {
                    Process[] processlist = Process.GetProcesses();

                    foreach (Process process in processlist)
                    {

                        if (!String.IsNullOrEmpty(process.MainWindowTitle))
                        {

                            if (process.MainWindowTitle == "Volumes - Docker Desktop")
                            {
                                dockerModel.Status = "OPEN";
                                break;
                            }
                            else
                            {
                                dockerModel.Status = "CLOSE";
                            }
                        }
                    }
                    Debug.WriteLine("DOCKER MODEL : " + Docker.Status);

                    Thread.Sleep(1000);
                }
            });
        }

    }

And here is my class ObservableObject who implement INotifyPropertyChanged :

class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

I have tried this but this don’t work do someone have a solution ?

Thank’s in advance
Nicolas

>Solution :

The DockerModel class should implement INotifyPropertyChanged and raise the PropertyChanged event for the data-bound Status property:

public string Status
{
    get => status;
    set
    {
        status = value;
        OnPropertyChanged(nameof(Status));
    }
}

Also, you should modify the Docker property of your view model to call OnPropertyChanged for the Docker property:

public DockerModel Docker
{
    get => dockerModel;
    set
    {
        if (dockerModel != value)
        {
            dockerModel = value;
            OnPropertyChanged(nameof(Docker));
        }
    }
}

It makes no sense to pass the value of dockerModel.Status to the OnPropertyChanged method. You will pass the name of the property to update.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading