WPF如何取Grid获取其中的Button实例。 100
一个Grid中有2行2列,每一格中都有一个button,如何根据行和列的值,获取单元格内的button实例?我找来找去,都不能同时使用这2个值,只有Grid.Column...
一个Grid中有2行2列,每一格中都有一个button,如何根据行和列的值,获取单元格内的button实例?
我找来找去,都不能同时使用这2个值,只有Grid.ColumnDefinitions[] 和 Grid.RowDefinitions[]这两个集合。我也想过用Grid.GetChild[],不过后续一些操作无法实现,所以,现在还是要试图直接这么找。
最好能写出类似这样一个方法
GetButton(string column_value,string row_value)
{
//怎么写?
return button;
} 展开
我找来找去,都不能同时使用这2个值,只有Grid.ColumnDefinitions[] 和 Grid.RowDefinitions[]这两个集合。我也想过用Grid.GetChild[],不过后续一些操作无法实现,所以,现在还是要试图直接这么找。
最好能写出类似这样一个方法
GetButton(string column_value,string row_value)
{
//怎么写?
return button;
} 展开
3个回答
推荐于2018-03-23 · 知道合伙人互联网行家
关注
展开全部
前端代码:
<Grid Name="mygrid">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0" Width="80" Height="25" Content="Button1"/>
<Button Grid.Row="0" Grid.Column="1" Width="80" Height="25" Content="Button2"/>
<Button Grid.Row="1" Grid.Column="0" Width="80" Height="25" Content="Button3"/>
<Button Grid.Row="1" Grid.Column="1" Width="80" Height="25" Content="Button4"/>
<StackPanel Grid.Row="2" Grid.ColumnSpan="2" Orientation="Horizontal" Margin="5">
<TextBox Name="tbRow" Height="20" Width="50"></TextBox>
<TextBox Name="tbColumn" Height="20" Width="50" Margin="5,0,0,0"></TextBox>
<Button Height="20" Content="click" Click="Button_Click" Margin="5,0,0,0"/>
<TextBlock Name="tbResult" Margin="20,0,0,0"/>
</StackPanel>
</Grid>
后台代码:
private void Button_Click(object sender, RoutedEventArgs e)
{
string row = tbRow.Text.ToString();
string column = tbColumn.Text.ToString();
GetButton(row, column);
}
public void GetButton(string column_value, string row_value)
{
int count = mygrid.Children.Count;
for (int i = 0; i < count; i++)
{
Button btn = mygrid.Children[i] as Button;
if (Grid.GetRow(btn) == int.Parse(row_value) && Grid.GetColumn(btn) == int.Parse(column_value))
{
tbResult.Text = btn.Content.ToString();
return;
}
}
}
<Grid Name="mygrid">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0" Width="80" Height="25" Content="Button1"/>
<Button Grid.Row="0" Grid.Column="1" Width="80" Height="25" Content="Button2"/>
<Button Grid.Row="1" Grid.Column="0" Width="80" Height="25" Content="Button3"/>
<Button Grid.Row="1" Grid.Column="1" Width="80" Height="25" Content="Button4"/>
<StackPanel Grid.Row="2" Grid.ColumnSpan="2" Orientation="Horizontal" Margin="5">
<TextBox Name="tbRow" Height="20" Width="50"></TextBox>
<TextBox Name="tbColumn" Height="20" Width="50" Margin="5,0,0,0"></TextBox>
<Button Height="20" Content="click" Click="Button_Click" Margin="5,0,0,0"/>
<TextBlock Name="tbResult" Margin="20,0,0,0"/>
</StackPanel>
</Grid>
后台代码:
private void Button_Click(object sender, RoutedEventArgs e)
{
string row = tbRow.Text.ToString();
string column = tbColumn.Text.ToString();
GetButton(row, column);
}
public void GetButton(string column_value, string row_value)
{
int count = mygrid.Children.Count;
for (int i = 0; i < count; i++)
{
Button btn = mygrid.Children[i] as Button;
if (Grid.GetRow(btn) == int.Parse(row_value) && Grid.GetColumn(btn) == int.Parse(column_value))
{
tbResult.Text = btn.Content.ToString();
return;
}
}
}
展开全部
private Button GetButton(int col, int row)
{
Button b = null;
for (int i = 0; i < grid1.Children.Count; i++)
{
if (grid1.Children[i] is Button)
{
int row1 =(int)(grid1.Children[i] as Button).GetValue(Grid.RowProperty);
int col1 = (int)(grid1.Children[i] as Button).GetValue(Grid.ColumnProperty);
if (col == col1 && row1 == row)
{
b = grid1.Children[i] as Button;
break;
}
}
}
return b;
}
{
Button b = null;
for (int i = 0; i < grid1.Children.Count; i++)
{
if (grid1.Children[i] is Button)
{
int row1 =(int)(grid1.Children[i] as Button).GetValue(Grid.RowProperty);
int col1 = (int)(grid1.Children[i] as Button).GetValue(Grid.ColumnProperty);
if (col == col1 && row1 == row)
{
b = grid1.Children[i] as Button;
break;
}
}
}
return b;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
最好是给button名字,找起来方便。
你可以这么试试,先找到所有button,然后判断每个button在哪个“格子”里(在哪行哪列好像是定义在该控件上的,而不是grid中的)
你可以这么试试,先找到所有button,然后判断每个button在哪个“格子”里(在哪行哪列好像是定义在该控件上的,而不是grid中的)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询