Dynamically (programmatically) created tiles changing its isEnabled property via a timer

54 Views Asked by At

Hey all I am not sure why I am having such a hard time with this but I am needing to be able to change a tiles isEnabled property depending on if my program detects an internet connection or not.

The WPF code that I am using and needing to change is the following:

<dxlc:TileLayoutControl x:Name="tileLayoutControl1" Margin="150,63,153,57" Padding="5"
   AllowAddFlowBreaksDuringItemMoving="False" AllowItemMoving="False" Orientation="Horizontal" ScrollBars="None"
   TileClick="tileLayoutControl1_TileClick">
</dxlc:TileLayoutControl>

This gets populated by the code behind on startup:

private void createMenu()
{
   List<String> menuIcons = new List<string>();
        
   menuIcons.Add("gamesIcon.png");
   menuIcons.Add("movieIcon.png");
   menuIcons.Add("musicIcon.png");
   menuIcons.Add("televisionIcon.png");
   menuIcons.Add("youTubeIcon.png");
   menuIcons.Add("androidIcon.png");

   foreach (String item in menuIcons)
   {
       Image finalImage = new Image();
       BitmapImage image = new BitmapImage();

       image.BeginInit();
       image.UriSource = new Uri("/img/" + item.ToString(), UriKind.Relative);
       image.EndInit();

       finalImage.Source = image;
       image = null;

       tileLayoutControl1.Children.Add(new Tile()
       {
           Content = finalImage,
           Name = item.ToString().Replace(".png", ""),
           Tag = item.ToString().Replace(".png", "").Replace(".jpg", ""),
           Width = 255,
           Height = 288,
           IsEnabled = false,
           Margin = new Thickness(0, 0, 50, 20),
           Background = Brushes.Transparent,
           BorderThickness = new Thickness(0, 0, 0, 0)
       }
    }
}

and finally the code that I have checking the internet connection within a timer:

private bool checkInternet()
{
    try
    {
        Ping myPing = new Ping();
        PingReply reply = myPing.Send("google.com", 1000, new byte[32], new PingOptions());

        return (reply.Status == IPStatus.Success);
    }
    catch (Exception)
    {
        return false;
    }
}

public firstWindow()
{
    InitializeComponent();
    var startTimeSpan = TimeSpan.Zero;
    var periodTimeSpan = TimeSpan.FromMinutes(5);

    var timer = new System.Threading.Timer((e) =>
    {
        bool hasInternet = checkInternet();

        TileLayoutControl test = (TileLayoutControl)this.tileLayoutControl1.FindName("youTubeIcon");

        test.IsEnabled = true;
        tileLayoutControl1.RegisterName("youTubeIcon", this.isEnabled = true);
    }, null, startTimeSpan, periodTimeSpan);
}

I am currently not getting any errors for the lines:

TileLayoutControl test = (TileLayoutControl)this.tileLayoutControl1.FindName("youTubeIcon");
test.IsEnabled = true;
tileLayoutControl1.RegisterName("youTubeIcon", this.isEnabled = true);

But test is null and RegisterName doesn't seem to change the isEnabled property when I try to click on the tile.

I've also read that using Command="{Binding ClickCommand}" may be my answer but that seems to only pertain to MVVM type of patterns which I am not using for this particle program.

I also know that checking the tileLayoutCibtril1 has a Children property that does contain my dynamically created icons but tileLayoutCibtril1.Children does not have a .FindName proeprty.

enter image description here

So what am I missing?

1

There are 1 best solutions below

0
user2250152 On BEST ANSWER

Try to call ApplyTemplate on TileLayoutControl after foreach statement. The template for TileLayoutControl has not yet been applied and therefore FindName returns null. ApplyTemplate builds the current template’s visual tree

foreach (String item in menuIcons)
{
    ...
}
tileLayoutControl1.ApplyTemplate();