【问题标题】:How to change UserControl property from static class?如何从静态类更改 UserControl 属性?
【发布时间】:2017-07-10 09:00:38
【问题描述】:

我在 MainWindow.xaml 中有 GridGrid 填充了我的UserControl(修改为Button)。

在静态类 Globals 中,我有 bool 变量,它在 Button 按下时发生变化。现在我还需要更改此布尔变量更改的 Grid 背景颜色。

问题是,我无法从 MainWindow.xaml.cs 后面的其他代码访问 Grid

Global.cs:

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

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

我的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;
            }

        }
    }

【问题讨论】:

  • 为什么不在你的点击方法中改变背景?
  • 你应该考虑遵循 MVVM 模式
  • 从 WPF 4.5 开始,您可以轻松bind to static properties。所以你可以简单地为网格的背景设置一个 DataTrigger。

标签: c# wpf xaml user-controls


【解决方案1】:

您可以使用Window.GetWindow 方法获取对UserControl 的父窗口的引用:

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;
    }
}

为了能够访问Grid,您可以在MainWindow.xaml 的XAML 标记中给它一个x:Name

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

【讨论】:

    【解决方案2】:

    如果您没有实现 MVVM 模式(或类似模式),您可以获取包含网格并设置颜色:

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-16
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多