WPF 获取 Stackpanel 子邻居
WPF Get Stackpanel children neighbors
我有一个堆栈面板,我根据在别处定义的二维数组动态添加了按钮。所以本质上它构建了一个按钮网格,但添加为堆栈面板的子项。我需要做的是根据列号和行号指定一个子按钮。这是堆栈面板。
<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
属性?
尝试以下操作:
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;
Grid pGrid = source.Parent as Grid;
if (pGrid == null) throw new Exception("Can't find parent grid.");
for (int i = 0; i < pGrid.Children.Count; i++) {
Button childButton = pGrid.Children[i] as Button;
if(childButton == null) continue;
int childCol = Grid.GetColumn(childButton);
int childRow= Grid.GetRow(childButton);
if(Math.Abs(childCol - col) < 2 && Math.Abs(childRow - row) < 2) childButton.Visibility = Visibility.Hidden;
}
}
这将遍历您的 children,如果它是 Button
的类型,它将检查 row
和 column
是否在您的 [=14= 旁边] 并将隐藏它。
我有一个堆栈面板,我根据在别处定义的二维数组动态添加了按钮。所以本质上它构建了一个按钮网格,但添加为堆栈面板的子项。我需要做的是根据列号和行号指定一个子按钮。这是堆栈面板。
<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
属性?
尝试以下操作:
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;
Grid pGrid = source.Parent as Grid;
if (pGrid == null) throw new Exception("Can't find parent grid.");
for (int i = 0; i < pGrid.Children.Count; i++) {
Button childButton = pGrid.Children[i] as Button;
if(childButton == null) continue;
int childCol = Grid.GetColumn(childButton);
int childRow= Grid.GetRow(childButton);
if(Math.Abs(childCol - col) < 2 && Math.Abs(childRow - row) < 2) childButton.Visibility = Visibility.Hidden;
}
}
这将遍历您的 children,如果它是 Button
的类型,它将检查 row
和 column
是否在您的 [=14= 旁边] 并将隐藏它。