string 转 label,button等空间类型

写代码过程中,遇到问题,要定义一个string ,用for循环增量string的序号,对应前台的不同label,由于string类型无法直接转换成label类型。如何转换呢?

for (int i = 1; i <= numleft; i++)
                        {
                            string str = "label" + i + "";
                            string but = "button_" + i + "";
                            Button bu = grid.FindName(but) as Button;
                            bu.Content = "4535353";
                            string st = "label" +( 20 + i) + "";
                            Label time = grid.FindName(st) as Label;
                            time.Content = "45534";
                            Label la= grid.FindName(str) as Label;
                           la.Content = "" + i + ".rtret" + i + "";
                        }

可以看到,我前台的label命名规则是label1,label2,label3......,要逐个对每个label进行Content赋值;由于label.name是string型,如何组个增量呢,具体的就看代码的实现了。前提是这些label要在同一个grid下面,具体的看前台代码:

<Grid  Name="grid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="761*" />
            <ColumnDefinition Width="19*" />
        </Grid.ColumnDefinitions>
        <!--<Image Name="image1" Stretch="Fill" Source="/Resources/Resources1/1.png" Grid.ColumnSpan="2" />-->
        <Label Height="28" Margin="0,24,0,0" Name="label1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="227" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
        <Label Height="28" HorizontalAlignment="Left" Margin="0,69.001,0,0" Name="label2" VerticalAlignment="Top" Width="227" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
        <Label Height="28" HorizontalAlignment="Left" Margin="0,112,0,0" Name="label3" VerticalAlignment="Top" Width="227" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
        <Label Height="28" HorizontalAlignment="Left" Margin="0,153,0,0" Name="label4" VerticalAlignment="Top" Width="227" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
        <Label Height="28" HorizontalAlignment="Left" Margin="0,196,0,0" Name="label5" VerticalAlignment="Top" Width="227" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
        <Label HorizontalAlignment="Left" Margin="0,242.717,0,229.283" Name="label6" Width="227" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
        <Label HorizontalAlignment="Left" Margin="0,0,0,190" Name="label7" Width="227" FocusVisualStyle="{x:Null}" Height="28" VerticalAlignment="Bottom" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
        <Label HorizontalAlignment="Left" Margin="0,0,0,144" Name="label8" Width="227" Height="28" VerticalAlignment="Bottom" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
        <Label Height="28" HorizontalAlignment="Left" Margin="0,0,0,103" Name="label9" VerticalAlignment="Bottom" Width="227" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
        <Label Height="26.716" HorizontalAlignment="Left" Margin="0,0,0,60.568" Name="label10" VerticalAlignment="Bottom" Width="227" FocusVisualStyle="{x:Null}" Foreground="Brown" FontSize="16px" Grid.ColumnSpan="2"></Label>
        <Label Height="28" Margin="0,24,153,0" Name="label11" VerticalAlignment="Top" FocusVisualStyle="{x:Null}" Grid.Column="1" Foreground="Brown" FontSize="16px" HorizontalAlignment="Right" Width="215"></Label>
        <Label Height="28" Margin="0,69,153,0" Name="label12" VerticalAlignment="Top" FocusVisualStyle="{x:Null}" Grid.Column="1" Foreground="Brown" FontSize="16px" HorizontalAlignment="Right" Width="215"></Label>
        <Label Height="28" Margin="0,112,153,0" Name="label13" VerticalAlignment="Top" FocusVisualStyle="{x:Null}" Grid.Column="1" Foreground="Brown" FontSize="16px" HorizontalAlignment="Right" Width="215"></Label>
        <Label Height="28" Margin="0,153,153,0" Name="label14" VerticalAlignment="Top" FocusVisualStyle="{x:Null}" Grid.Column="1" Foreground="Brown" FontSize="16px" HorizontalAlignment="Right" Width="215"></Label>
        <Label Height="28" Margin="0,197.717,153,0" Name="label15" VerticalAlignment="Top" FocusVisualStyle="{x:Null}" Grid.Column="1" Foreground="Brown" FontSize="16px" HorizontalAlignment="Right" Width="215"></Label>
        <Label Margin="0,242.717,153,229.283" Name="label16" FocusVisualStyle="{x:Null}" Grid.Column="1" Foreground="Brown" FontSize="16px" HorizontalAlignment="Right" Width="215"></Label>
     

posted on 2012-09-26 15:12  秋毫  阅读(604)  评论(0编辑  收藏  举报

导航