Odwołanie do konkretnego buttona

0

Witam serdecznie. Natrafiłem na mały problem. Próbuje odwołać się do odpowiedniego buttona.

public void CreatePanel()
        {
            StackPanel SeriesPanelUI = new StackPanel();
            SeriesPanelUI.Height = 80;
            SeriesPanelUI.Orientation = Orientation.Horizontal;
            SeriesPanelUI.HorizontalAlignment = HorizontalAlignment.Center;
            SeriesPanelUI.UseLayoutRounding = true;

            SeriesPanelUI.Holding += new HoldingEventHandler(SeriesPanelUI_Holding);


            StackPanel AppBarButtonPanelUI = new StackPanel();
            AppBarButtonPanelUI.Orientation = Orientation.Vertical;
            AppBarButtonPanelUI.VerticalAlignment = VerticalAlignment.Top;
            AppBarButtonPanelUI.HorizontalAlignment = HorizontalAlignment.Center;
            AppBarButtonPanelUI.Margin = new Thickness(0, 0, 0, 0);

            AppBarButton AppBarButtonDeleteUI = new AppBarButton(); // do niego chciałbym się odwołać
            AppBarButtonDeleteUI.Icon = new SymbolIcon(Symbol.Delete);
            AppBarButtonDeleteUI.HorizontalAlignment = HorizontalAlignment.Center;
            AppBarButtonDeleteUI.VerticalAlignment = VerticalAlignment.Center;
            AppBarButtonDeleteUI.Margin = new Thickness(0, 10, 0, 0);
            AppBarButtonDeleteUI.Height = 60;
            AppBarButtonDeleteUI.IsEnabled = false;

            AppBarButtonDeleteUI.Click += new RoutedEventHandler(ButtonDelete_Click);            


            Border BorderUI = new Border();
            BorderUI.Height = 40;
            BorderUI.Width = 40;
            BorderUI.Margin = new Thickness(0, 0, 0, 0);

            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.UriSource = new Uri("ms-appx:///Assets/Image.bmp");

            Image ImageUI = new Image();
            ImageUI.Height = 40;
            ImageUI.Source = bitmapImage;

            BorderUI.Child = ImageUI;


            StackPanel FlyoutButtonPanelUI = new StackPanel();
            FlyoutButtonPanelUI.Orientation = Orientation.Vertical;
            FlyoutButtonPanelUI.VerticalAlignment = VerticalAlignment.Top;
            FlyoutButtonPanelUI.HorizontalAlignment = HorizontalAlignment.Center;
            FlyoutButtonPanelUI.Margin = new Thickness(0, 0, 0, 0);

            Button ButtonUI = new Button();                                     
            ButtonUI.HorizontalAlignment = HorizontalAlignment.Stretch;
            ButtonUI.VerticalAlignment = VerticalAlignment.Center;
            ButtonUI.FontSize = 14;
            ButtonUI.Margin = new Thickness(10, 10, 0, 0);
            ButtonUI.Height = 40;
            ButtonUI.IsEnabled = false;

            Flyout FlyoutUI = new Flyout();
            ButtonUI.Flyout = FlyoutUI;


            StackPanel TextBlockPanelUI = new StackPanel();
            TextBlockPanelUI.Orientation = Orientation.Vertical;
            TextBlockPanelUI.VerticalAlignment = VerticalAlignment.Top;
            TextBlockPanelUI.Margin = new Thickness(0, 0, 0, 0);
            TextBlockPanelUI.HorizontalAlignment = HorizontalAlignment.Center;

            TextBlock TextBlockUI = new TextBlock();
            TextBlockUI.TextWrapping = TextWrapping.Wrap;
            TextBlockUI.Text = "0";
            TextBlockUI.Width = 100;
            TextBlockUI.FontSize = 40;
            TextBlockUI.HorizontalAlignment = HorizontalAlignment.Center;
            TextBlockUI.VerticalAlignment = VerticalAlignment.Center;
            TextBlockUI.Margin = new Thickness(10, 10, 0, 0);

            TextBlockPanelUI.Children.Add(TextBlockUI);


            StackPanel AppBarButtonPlusPanelUI = new StackPanel();
            AppBarButtonPlusPanelUI.Orientation = Orientation.Vertical;
            AppBarButtonPlusPanelUI.VerticalAlignment = VerticalAlignment.Top;
            AppBarButtonPlusPanelUI.Margin = new Thickness(0, 0, 0, 0);
            AppBarButtonPlusPanelUI.HorizontalAlignment = HorizontalAlignment.Center;

            AppBarButton AppBarButtonPlusUI = new AppBarButton();                             
            AppBarButtonPlusUI.Icon = new SymbolIcon(Symbol.Add);
            AppBarButtonPlusUI.HorizontalAlignment = HorizontalAlignment.Stretch;
            AppBarButtonPlusUI.VerticalAlignment = VerticalAlignment.Stretch;
            AppBarButtonPlusUI.Margin = new Thickness(0, 10, 0, 0);
            AppBarButtonPlusUI.Height = 60;

            AppBarButtonPlusPanelUI.Children.Add(AppBarButtonPlusUI);


            StackPanel AppBarButtonMinusPanelUI = new StackPanel();
            AppBarButtonMinusPanelUI.Orientation = Orientation.Vertical;
            AppBarButtonMinusPanelUI.VerticalAlignment = VerticalAlignment.Top;
            AppBarButtonMinusPanelUI.Margin = new Thickness(0, 0, 0, 0);
            AppBarButtonMinusPanelUI.HorizontalAlignment = HorizontalAlignment.Center;

            AppBarButton AppBarButtonMinusUI = new AppBarButton();                              
            AppBarButtonMinusUI.Icon = new SymbolIcon(Symbol.Remove);
            AppBarButtonMinusUI.HorizontalAlignment = HorizontalAlignment.Stretch;
            AppBarButtonMinusUI.VerticalAlignment = VerticalAlignment.Stretch;
            AppBarButtonMinusUI.Margin = new Thickness(0, 10, 0, 0);
            AppBarButtonMinusUI.Height = 60;

            AppBarButtonMinusPanelUI.Children.Add(AppBarButtonMinusUI);


            SeriesPanelUI.Children.Add(AppBarButtonPanelUI);
            SeriesPanelUI.Children.Add(BorderUI);
            SeriesPanelUI.Children.Add(FlyoutButtonPanelUI);
            SeriesPanelUI.Children.Add(TextBlockPanelUI);
            SeriesPanelUI.Children.Add(AppBarButtonPlusPanelUI);
            SeriesPanelUI.Children.Add(AppBarButtonMinusPanelUI);            


            listView.Items.Add(SeriesPanelUI);
        }
        private void SeriesPanelUI_Holding(object sender, HoldingRoutedEventArgs e)
        {
            AppBarButton BT = sender as AppBarButton;

            BT.IsEnabled = true;                  
        }

Próbuje użyć delegata ale mam kilka przycisków appbarButton i nie wiem jak to odpowiednio przydzielić. Proszę o pomoc :)

1

Udało mi się rozwiązać problem za pomocą tej funkcji:

 public T FindElementByName<T>(DependencyObject element, string sChildName) where T : FrameworkElement
        {
            T childElement = null;
            var nChildCount = VisualTreeHelper.GetChildrenCount(element);
            for (int i = 0; i < nChildCount; i++)
            {
                FrameworkElement child = VisualTreeHelper.GetChild(element, i) as FrameworkElement;

                if (child == null)
                    continue;

                if (child is T && child.Name.Equals(sChildName))
                {
                    childElement = (T)child;
                    break;
                }

                childElement = FindElementByName<T>(child, sChildName);

                if (childElement != null)
                    break;
            }
            return childElement;
        }

Oraz jej użycie:

Button Button_Exercise = FindElementByName<Button>(listView, "Button_Exercise");

1 użytkowników online, w tym zalogowanych: 0, gości: 1