A
A
Alexey2016-01-22 11:05:48
WPF
Alexey, 2016-01-22 11:05:48

Why doesn't the interface reflect changes?

There is a form which Grid is broken into cells.
I dynamically fill the form with content, placing a set of elements in each cell of the Grid.
This happens in a loop, the contents of the loop are below:

DockPanel dp = new DockPanel();
                        TextBlock tb = new TextBlock();
                        Viewbox vb = new Viewbox();
                        TextBlock tbvb = new TextBlock();

                        Binding bind = new Binding();
                        bind.Source = MyDivList[n].FontCollor;
                        dp.SetBinding(DockPanel.BackgroundProperty, bind);

                        Binding bind2 = new Binding();
                        bind2.Source = MyDivList[n].Status.ToString();
                        tb.SetBinding(TextBlock.TextProperty, bind2);

                        Binding bind3 = new Binding();
                        bind3.Source = MyDivList[n].Name;
                        tbvb.SetBinding(TextBlock.TextProperty, bind3);

                        // теперь связываем 
                        // сначала Viewbox и TextBlock
                        vb.Child = tbvb;

                        // теперь связываем DockPanel и TextBlock
                        dp.Children.Add(tb);
                        DockPanel.SetDock(tb, Dock.Bottom);
                        dp.Children.Add(vb);

                        MyGrid.Children.Add(dp);
                        Grid.SetRow(dp, i);
                        Grid.SetColumn(dp, j);

As I build the elements, I bind the binding to a specific entry in the ObservableCollection MyDivList.
Division class below.
public class Division : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string _Name;
        private string _L_Ip;
        private string _VPN_Ip;
        private int _Status;
        private string _Message;


        public string Name { 
                                        get { return _Name; }
                                         set { _Name = value; 
                                                RaisePropertyChanged("Name"); 
                                               }
                                      }
        public string L_Ip
                            {
                                get { return _L_Ip; }
                                set { _L_Ip = value; 
                                      RaisePropertyChanged("L_Ip"); 
                                    }
                            }
        public string VPN_Ip
                            {
                                get { return _VPN_Ip; }
                                set { _VPN_Ip = value; 
                                      RaisePropertyChanged("VPN_Ip"); 
                                     }
                            }

        // статусы 
        public int Status
                            {
                                get { return _Status; }
                                set { _Status = value; 
                                      RaisePropertyChanged("Status");
                                      RaisePropertyChanged("FontCollor"); 
                                    }
                            }
        public string Message
                            {
                                get { return _Message; }
                                set { _Message = value; 
                                      RaisePropertyChanged("Message"); 
                                    }
                            }
        public SolidColorBrush FontCollor 
        { 
            
            get {

                switch (Status)
                {
                    case 1: return   new SolidColorBrush(Colors.Red);  // не пингуется сервер магазина

                    case 2: return new SolidColorBrush(Colors.Yellow);  // не пингуется компьютер бухгалтера
                    
                    case 3: return new SolidColorBrush(Colors.LightGoldenrodYellow);  // не пингуется компьютер управляющей

                    case 4: return new SolidColorBrush(Colors.Blue);  // не работает УТМ

                    default:  return new SolidColorBrush(SystemColors.ControlColor);
                }
                
                }
        }

        protected virtual void RaisePropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, e);
        }

        protected void RaisePropertyChanged(string propertyName)
        {
            RaisePropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
    }

As a result, after filling the collection with data and working out the code, I get this form (the phrase about advertising is a joke in the program)
fff51e44fef04e7d8745ab9839674e68.png
According to my logic, after checking that the unit is offline, I change its status to 1. And the cell will have to change color to another. But it didn't work out the way I wanted. Despite the fact that the data in the collection changes, the form does not change the message, but the color does not change.
I ask you to tell me where I made a mistake, and in general whether I did the right thing or not.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2016-01-22
@k1lex

For the DockPanel, set the DataContext to the corresponding Division instance
. In all bindings inside the DockPanel, do not fill in the source, but only set the Path - to the corresponding property. Thus, all bindings will be attached to the Division and take the appropriate properties from it. As soon as PropertyChanged is triggered, the corresponding text, color, etc. will also change.
It is more convenient to write such templates through Xaml.
You can go further and arrange the entire table through ItemsControl (your Division array in ItemsSource), in the ItemTemplate of which your DockPanel is registered. And then you do not need to organize a cycle to create elements - they will be created by themselves.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question