Answer the question
In order to leave comments, you need to log in
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);
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));
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question