Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 一个按钮中网格内的两个标签,如何在WPF中以编程方式访问它们_C#_Wpf_Button_Labels - Fatal编程技术网

C# 一个按钮中网格内的两个标签,如何在WPF中以编程方式访问它们

C# 一个按钮中网格内的两个标签,如何在WPF中以编程方式访问它们,c#,wpf,button,labels,C#,Wpf,Button,Labels,所以我有一个有很多按钮的网格,我需要按钮有两个不同的标签,所以我把它们添加到网格中 <Button x:Name="Bid_0" Background="Blue" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Opacity=".7" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.ColumnSpan="2"

所以我有一个有很多按钮的网格,我需要按钮有两个不同的标签,所以我把它们添加到网格中

 <Button x:Name="Bid_0" Background="Blue" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"  Opacity=".7" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.ColumnSpan="2"  Grid.Column="2" Grid.Row="5"  Grid.RowSpan="2" Padding="0">
  <Grid  x:Name="Bid_0_Grid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Grid.RowDefinitions>
      <RowDefinition Height="*"/>
      <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Label x:Name="Bid_Price_0" Grid.Row="0"  Grid.ColumnSpan="2" HorizontalAlignment="Stretch"  UseLayoutRounding="True" Padding="0"/>
    <Label x:Name="Bid_Vol_0" Grid.Row="1"  Grid.Column="1" HorizontalAlignment="Stretch"  UseLayoutRounding="True" Padding="0"/>
  </Grid>
</Button>
因此,当我有单独的按钮时,我需要一种从代码中访问标签的方法

或者,我可以迭代我的主网格以获得按钮内部的所有网格吗


谢谢你的帮助

无需将它们添加到网格中。您可以从代码隐藏中访问按钮的内容属性。在您的情况下,例如:Bid_0.Content=“您的标签”
或者,如果遵循MVVM设计,您甚至可以绑定按钮内容

无需将它们添加到网格中。您可以从代码隐藏中访问按钮的内容属性。在您的情况下,例如:Bid_0.Content=“您的标签”
或者,如果遵循MVVM设计,您甚至可以绑定按钮内容

这可能不是最好的解决方案。但是,如果遵循命名约定,在按钮名称后加上“_Grid”,则可以执行此操作。我肯定你想要更优雅的

  Grid myGrid = (Grid)this.FindName(button.Name+"_Grid");
        foreach (Control c in myGrid.Children )
        {
            if (c.GetType() == typeof(Label))
            {
                Label l = c;
                l.Content = "text";
            }
        }

这可能不是最好的解决办法。但是,如果遵循命名约定,在按钮名称后加上“_Grid”,则可以执行此操作。我肯定你想要更优雅的

  Grid myGrid = (Grid)this.FindName(button.Name+"_Grid");
        foreach (Control c in myGrid.Children )
        {
            if (c.GetType() == typeof(Label))
            {
                Label l = c;
                l.Content = "text";
            }
        }

我将创建一个UserControl并将我的按钮放在其中。然后我将把UserControl嵌入主网格中。这样我就不需要复杂的命名约定了

我会创建一个UserControl并将我的按钮放在其中。然后我将把UserControl嵌入主网格中。这样我就不需要复杂的命名约定了

投标价格0.Content=“text”;或者,根据上面的代码,您正在询问如何根据枚举时打开的当前按钮访问它们。。我猜是这样的。我只是想确认一下,也许我不清楚。我有40个按钮,需要遍历它们并设置两个标签,当我有按钮时您可以使用多个标签管理40个按钮,而不使用MVVM?听起来好像有很多意大利面代码……Bid_Price_0.Content=“text”;或者,根据上面的代码,您正在询问如何根据枚举时打开的当前按钮访问它们。。我猜是这样的。我只是想确认一下,也许我不清楚。我有40个按钮,需要遍历它们并设置两个标签,当我有按钮时您可以使用多个标签管理40个按钮,而不使用MVVM?对我来说,听起来像是很多意大利面条式的代码…这不起作用,因为我需要在每个按钮中有两个不同的动态内容。当我只需要设置一个不起作用的东西,因为我需要在每个按钮中有两个不同的动态内容时,我就这样做了。当我只有一件事要做的时候,我正在做这件事