【问题标题】:WPF rectangle color bindingWPF 矩形颜色绑定
【发布时间】:2011-04-01 09:25:07
【问题描述】:

我正在尝试编写矩形网格,它确实会改变其对象的颜色。

  private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i < size; i++)
        {
            main_grid.ColumnDefinitions.Add(new ColumnDefinition());
            main_grid.RowDefinitions.Add(new RowDefinition());
        }
        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                cells[i, j] = new Cell { state = false, col = false };
                Rectangle rect = new Rectangle();
                Grid.SetColumn(rect, j);
                Grid.SetRow(rect, i);
                rect.Fill = Brushes.Orange;
                rect.DataContext = cells[i, j];
                rect.SetBinding(OpacityProperty, "ev_opacity");
                Binding binding = new Binding("ev_col");
                binding.Converter = new BooleanToBrushConverter();
                rect.SetBinding(Rectangle.FillProperty, binding);
                main_grid.Children.Add(rect);
            }
        }
        setupTimer();
    }

如何根据 col 设置矩形的颜色? (例如:真 - 黑,假 - 白)

单元类:

class Cell : INotifyPropertyChanged
    {
        private bool _state;
        private bool _Col;
        public event PropertyChangedEventHandler PropertyChanged;
        public event PropertyChangedEventHandler PropertyChanged2;
        public bool Col; //to set color
        {
            get
            {
                return _Col;
            }
            set
            {
                _Col = value;
                if (PropertyChanged2 != null)
                {
                    PropertyChanged2(this, new PropertyChangedEventArgs("event2"));
                };  
            }
        }
        public bool state //to set opacity
        {
            get
            {
                return _state;
            }
            set
            {
                _state = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("ev_opacity"));
                };
            }
        }
        public static implicit operator int(Komorka c)
        {
            return Convert.ToInt32(c.state);
        }
    }

编辑: 这段代码不起作用 - 如果我点击网格,运行后什么也不会发生。

【问题讨论】:

    标签: c# wpf events binding colors


    【解决方案1】:

    一种常见的方法是在绑定上使用ValueConverter

    只需制作一个将布尔值转换为画笔的 ValueConverter。

    在 WPF 中,如果您有 CellControl,您还可以在单​​元格的模板中使用 DataTrigger

    编辑

    在代码中添加绑定:

        Binding binding = new Binding("State");
        binding.Converter = new BooleanToBrushConverter();
        _rectangle.SetBinding(Rectangle.FillProperty, binding);
    

    【讨论】:

    • 好的,我明白了,但是如果我有转换器,如何在 cs 代码中绑定矩形和布尔值?我在 xaml 文件中没有矩形,所以我不能这样做。
    • 请添加显示添加点击事件的代码。您是否附加了点击事件?
    【解决方案2】:

    绑定:

    my_rect.SetBinding(Rectangle.OpacityProperty, "state_opacity");
    my_rect.SetBinding(Rectangle.FillProperty,
                         new Binding()
                         {
                             Converter = new BooleanToBrushConverter(),
                             Path = new PropertyPath("state_color")
                         }
                      );
    

    细胞类。矩形颜色的变化取决于“state_color”变量。

    class Komorka : INotifyPropertyChanged
        {
            private bool _state_opacity;
            private bool _state_color;
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            public bool state_color
            {
                get { return _state_color; }
                set
                {
                    _state_color = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("state_color"));
                    }; 
                }
            }
    
            public bool state_opacity
            {
                get
                {
                    return _state_opacity;
                }
                set
                {
                    _state_opacity = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("state_opacity"));
                    };
                }
            }
        }
    }
    

    转换器类:

    class BoolToBrush : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value != null)
                {
                    if ((bool)value == false)
                    {
                        return new SolidColorBrush(Colors.Orange);
                    }
                    else
                    {
                        return new SolidColorBrush(Colors.Black);
                    }
                }
                else
                {
                    return new SolidColorBrush(Colors.Red);
                }
            }
    

    【讨论】:

    • 这个答案不太清楚你是如何解决点击事件的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 2012-07-03
    • 2012-08-27
    • 2021-11-10
    • 2012-06-20
    • 1970-01-01
    相关资源
    最近更新 更多