我有一個基於其他地方定義的2D數組的動態添加按鈕的堆疊面板。所以本質上它構建了一個按鈕的網格,但添加爲堆棧面板的子節點。我需要做的是,根據列和行號指定一個子按鈕。這是Stackpanel。WPF獲取Stackpanel兒童鄰居
<StackPanel Name="myArea" HorizontalAlignment="Center" VerticalAlignment="Center"/>
這裏是一些有問題的代碼。
Grid values = new Grid();
values.Width = 320;
values.Height = 240;
values.HorizontalAlignment = HorizontalAlignment.Center;
values.VerticalAlignment = VerticalAlignment.Center;
int x = valueBoard.GetLength(0);
int y = valueBoard.GetLength(1);
for (int i = 0; i < x; i++)
{
ColumnDefinition col = new ColumnDefinition();
values.ColumnDefinitions.Add(col);
}
for (int j = 0; j < y; j++)
{
RowDefinition row = new RowDefinition();
values.RowDefinitions.Add(row);
}
for (int i = 0; i < x; i++) for (int j = 0; j < y; j++)
{
Button button = new Button();
button.Content = "";
button.Click += ButtonClick;
button.MouseRightButtonUp += RightClick;
Grid.SetColumn(button, i);
Grid.SetRow(button, j);
values.Children.Add(button);
}
myArea.Children.Add(values);
所以,現在,點擊一個按鈕,我想隱藏網格中所有相鄰的按鈕。這裏是我的單擊事件:
private void ButtonClick(object sender, RoutedEventArgs e)
{
int col = Grid.GetColumn((Button)sender);
int row = Grid.GetRow((Button)sender);
Button source = e.Source as Button;
source.Visibility = Visibility.Hidden;
}
我怎麼會在ButtonClick
,隱藏與col
和row
值鄰居按鈕?是否有某種類型的getter或setter比我可以將這些值送入並編輯鄰居的Visibility
屬性?
你在做一些遊戲嗎?如果是,請嘗試一些自定義面板,如果需要,我可以爲您找到一些樣本。 –