Ondřej Michejda
Ondřej Michejda

Reputation: 158

How to change UserControl property from static class?

I have Grid in MainWindow.xaml. Grid is filled with my UserControl (modified Button).

In static class Globals, i have bool varible, which is changing at Button press. now I need to change also Grid background color on this bool variable change.

Trouble is, I can't reach Grid from other then MainWindow.xaml.cs code behind.

Global.cs:

public static class Globals
    {
        private static bool _player;
        public static bool Player {
            get { return _player; }

            set {
                _player = value;
                Debug.WriteLine(value);
            }
        }
    }

My UserControl:

public partial class tetrisButton : UserControl
    {
        public tetrisButton()
        {
            InitializeComponent();
            Button.Focusable = false;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(!Globals.Player)
            {
                Button.Content = new cross();
                Globals.Player = true;
            }
            else
            {
                Button.Content = new circle();
                Globals.Player = false;
            }

        }
    }

Upvotes: 0

Views: 713

Answers (2)

mm8
mm8

Reputation: 169150

You can get a reference to the parent window of the UserControl using the Window.GetWindow method:

private void Button_Click(object sender, RoutedEventArgs e)
{
    MainWindow mainWindow = Window.GetWindow(this) as MainWindow;
    if (!Globals.Player)
    {
        Button.Content = new cross();
        Globals.Player = true;

        if (mainWindow != null)
            mainWindow.grid.Background = Brushes.Green;
    }
    else
    {
        Button.Content = new circle();
        Globals.Player = false;

        if (mainWindow != null)
            mainWindow.grid.Background = Brushes.Red;
    }
}

To be able to access the Grid, you could give it an x:Name in your XAML markup of MainWindow.xaml:

<Grid x:Name="grid" ... />

Upvotes: 2

Romano Zumb&#233;
Romano Zumb&#233;

Reputation: 8079

If you don't implement MVVM pattern (or similar) you could just get the containing grid and set the color:

public partial class tetrisButton : UserControl
{
    public tetrisButton()
    {
        InitializeComponent();
        Button.Focusable = false;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Grid parent = FindParent<Grid>(this);

        if(!Globals.Player)
        {
            Button.Content = new cross();
            Globals.Player = true;
            parent.Background = Brushes.Blue;
        }
        else
        {
            Button.Content = new circle();
            Globals.Player = false;
            parent.Background = Brushes.Red;
        }

    }

    private T FindParent<T>(DependencyObject child) where T : DependencyObject
    {
         T parent = VisualTreeHelper.GetParent(child) as T;

         if (parent != null)
             return parent;
         else
             return FindParent<T>(parent);
    }
}

Upvotes: 1

Related Questions