I'm writing a uwp program, where I want to bind a ObservableCollection to visibility of an Ellipse in a Ellipse matrix. Since I want to make the number of ellipses flexible, I initialize and bind them in the c# initialization code of the page. The problem is the ellipses' visibilities reflect the bool values which I set before the binding happens, but when the bool value changes after binding, the visibilities don't change with the bool value's change.
I declare the bool values in the App.xaml.cs
public int gameRow;`enter code here`
public int gameColumn;
public ObservableCollection<bool> gameMatrix;
In the page of ellipses, I declare a Grid to hold the ellipses in the GamePage.xaml.
<Grid Grid.Column="1" Grid.Row="0" x:Name="gameFlat"/>
In the corresponding GamePage.xaml.cs, I initialized ellipses, bind them to the bool values, and put them in a row/column in the gameFlat grid.
private void addEllipse(int i, int j, Binding[,] ellipseBindings)
{
ellipseBindings[i,j] = new Binding();
ellipseBindings[i, j].Source = gameMatrix[i*gameColumn+ j];
if (converter==null)
converter = new VisibilityConverter();
ellipseBindings[i, j].Converter = converter;
circles[i, j] = new Ellipse();
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Windows.UI.Color.FromArgb(255, 255, 106, 106);
circles[i, j].Fill = mySolidColorBrush;
circles[i, j].SetBinding(VisibilityProperty, ellipseBindings[i, j]);
Grid.SetRow(circles[i, j], i);
Grid.SetColumn(circles[i, j], j);
gameFlat.Children.Add(circles[i, j]);
}
The circles and the ellipseBinds are declared in the GamePage.xaml.cs as well.
Ellipse[,] circles;
Binding[,] EllipseBindings;
VisibilityConverter converter;
The visibility of ellipses are controlled by the bool values in the first time, but when I try to change the bool value after the binding, in a time tick function of a timer, the change of bool values don't affect the ellipses.
private void timer_tick(object sender, object e)
{
int testCount = pkgCountDown % (gameRow * gameColumn);
gameMatrix[(testCount % gameRow)* gameColumn
+ pkgCountDown% gameColumn ] = true;
gameCountDown--;
pkgCountDown--;
}
What's the wrong place in my code? Could you help me to make it work? Thank you!
-------update 2018/06/07--------
I tried to wrap the bool values in a class called isShownNotify which implements the INotifyPropertyChanged interface like below:
public class isShownNotify : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool _isShown;
public bool isShown
{
get { return _isShown; }
set
{
if (_isShown != value)
{
isShown = value;
}
OnPropertyChanged();
}
}
public void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string name = "")
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler!=null)
handler(this, new PropertyChangedEventArgs(name));
}
}
Then I changed the converter like below:
object IValueConverter.Convert(object value, Type targetType, object parameter, string language)
{
Type objectType = value.GetType();
PropertyInfo objectInfo = objectType.GetProperty("isShown");
bool boolValue = (bool)objectInfo.GetValue(value, null);
if (boolValue == true)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
But in the running of the program, the changes on bool values still don't change the visibility of my Ellipses.
When I set breakpoints in the OnPropertyChanged() function, I see that the value of PropertyChanged(the PropertyChangedEventHandler) is null. What's wrong with it? Do I bind it to the visibility of ellipses in a wrong way?