C# WPF Access from ViewModel to MainWindow

Markus Freitag 3,786 Reputation points
2020-07-16T12:48:30.113+00:00

Hello,
I have this.

 public class ViewModel : INotifyPropertyChanged
    {

The goal is to get access from ViewModel to MainWindow. So I can change the color and so one.

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();



            //DataContext.
            //((ViewModel)DataContext).SetLogger(Log);
            this.txtGebindeSlot1.Background = Brushes.Red;
        }

 private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,691 questions
{count} votes

Accepted answer
  1. Markus Freitag 3,786 Reputation points
    2020-07-17T19:16:37.87+00:00
    namespace Station  
    {  
        [ValueConversion(typeof(string), typeof(SolidColorBrush))]  
      
        public class ColorConverter : IValueConverter  
        {  
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
            {  
      
                string state = (string)value;  
                if ( state.Length > 0 )  
                    return new SolidColorBrush(Colors.Green);  
                else  
                    return new SolidColorBrush(Colors.Red);  
    

    Hello, Do you mean like this.

    The txtGebindeSlot1 control should bind the BackGround to a property (SolidColor) and when you set that property in code then the UI will use the value.

    Can you make a sample for me? Thanks.

    The code works not.
    See here12830--xx.jpg


2 additional answers

Sort by: Most helpful
  1. DaisyTian-1203 11,616 Reputation points
    2020-07-17T07:09:58.467+00:00

    Part 1: I will give you very simple demo of MVVM binding ViewModel to View. There are two ways of binding:

      //Method1 to bing Viewmodel to view
         PersonViewModel _viewModel = new PersonViewModel();
         DataContext = _viewModel;
        <!--Method2 to bing Viewmodel to view-->
            <!--<Window.DataContext>
                <local:PersonViewModel></local:PersonViewModel>
          </Window.DataContext>-->
    

    Part 2 : Use log4net to write log for the app:

    Step1:Search and install log4net in your project Nuget.
    Step2:Configure the config for your project.
    Step3:Add [assembly: log4net.Config.XmlConfigurator(Watch = true)] for your AssemblyInfo.cs.
    Step4:Add following code in your App.xaml.cs to log the start/end of project.
    Step5:Write the log in the project.

    Here is my demo for you.

    2 people found this answer helpful.

  2. Lloyd Sheen 1,391 Reputation points
    2020-07-16T21:20:33.68+00:00

    I can only suggest that you use WPF as it is supposed to be used and stop trying to use WinForms techniques.

    A ViewModel is just that. It has properties that the UI uses to set background colours , fonts etc. The txtGebindeSlot1 control should bind the BackGround to a property (SolidColor) and when you set that property in code then the UI will use the value.

    I am speaking from experience. I spent a while with WPF and eventually found that using WPF the correct way was much simpler and the code is much easier to deal with.

    1 person found this answer helpful.
    0 comments No comments