如何动态获取标签名称?

时间:2012-07-04 03:48:35

标签: c# wpf

<Grid x:Name="LayoutRoot">
    <TextBox x:Name="txt_diplay_1" HorizontalAlignment="Left" Height="42" Margin="155,78,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="103.5" GotFocus="txt_diplay_1_GotFocus" />
    <TextBox x:Name="txt_diplay_2" Height="42" Margin="297,78,239.5,0" TextWrapping="Wrap" VerticalAlignment="Top" GotFocus="txt_diplay_2_GotFocus" />
    <Button x:Name="btn_a" Content="A" HorizontalAlignment="Left" Height="40" Margin="155,147,0,0" VerticalAlignment="Top" Width="73" Click="btn_a_Click" />
    <Button x:Name="btn_b" Content="B" Height="40" Margin="237,147,0,0" VerticalAlignment="Top" Click="btn_b_Click" HorizontalAlignment="Left" Width="73" />
    <Button x:Name="btn_c"  Height="40" Margin="0,147,239.5,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="73" Click="btn_c_Click" >
        <Grid Height="30.833" Width="61.5">
            <Label x:Name="lbl_1" Content="1" Margin="22.498,6.5,19.501,2.166"/>
            <Label x:Name="lbl_2" Content="!" HorizontalAlignment="Right" Margin="0,-4.422,0,13.088" Width="19.501"/>
        </Grid>
    </Button>
</Grid>

设计就像这样

Result

public partial class MainWindow : Window
{
    Control TexboxDetails = null;      
    Control ButtonDetails;             
    Button BehaveButton;     

    public MainWindow()
    {
        this.InitializeComponent();
    }

private void btn_a_Click(object sender, RoutedEventArgs e)
    {
        ButtonDetails = (Control)sender;
        all_in_one();
    }

    private void btn_b_Click(object sender, RoutedEventArgs e)
    {
        ButtonDetails = (Control)sender;

        all_in_one();
    }

    private void btn_c_Click(object sender, RoutedEventArgs e)
    {

    }

    private void txt_diplay_1_GotFocus(object sender, RoutedEventArgs e)
    {
        TexboxDetails = (Control)sender;
    }

    private void txt_diplay_2_GotFocus(object sender, RoutedEventArgs e)
    {
        TexboxDetails = (Control)sender;
    }

    public void all_in_one()
    {
        BehaveButton = ButtonDetails as Button;
        if (TexboxDetails != null)
        {
            TextBox BehaveTextbox = TexboxDetails as TextBox;
            var caret_index = BehaveTextbox.CaretIndex;
            BehaveTextbox.Text = BehaveTextbox.Text.Insert(caret_index, BehaveButton.Content.ToString());
            BehaveTextbox.Focus();
            BehaveTextbox.CaretIndex = caret_index + 1;
        }
    }
}

使用上面的代码,我可以在单击该按钮时动态获取Button名称。 在上图中,一个按钮(btn_c)有两个标签。现在我想在单击按钮(btn_c)时获得单独的标签名称。

2 个答案:

答案 0 :(得分:1)

你可以这样得到它们(在 btn_c 点击处理程序中):

var btn_c = (Button)sender;
Grid grid = (Grid)btn_c.Content;
Label label1 = (Label)grid.Children[0];
string name1 = label1.Name;

答案 1 :(得分:0)

您的整个设计确实可以使用一些返工。看看这段代码:(注意事件处理程序的数量减少;你需要修改XAML才能使用这些代码)

public partial class MainWindow : Window
{
    TextBox LastFocusedTextBox;
    public MainWindow()
    {
        this.InitializeComponent();
    }

    private void btn_Click(object sender, RoutedEventArgs e)
    {
        InsertButtonContent((Button)sender);
    }

    private void txt_diplay_GotFocus(object sender, RoutedEventArgs e)
    {
        LastFocusedTextBox = (TextBox)sender;
    }

    public void InsertButtonContent(Button button)
    {
        if (LastFocusedTextBox != null)
        {
            string buttonContentString = button.Content as string;
            if (string.IsNullOrEmpty(buttonContentString))
            {
                var grid = button.Content as Grid;
                if (grid != null)
                    buttonContentString = string.Join("", grid.Children.OfType<ContentControl>().Select(x => x.Content));
            }
            var caret_index = LastFocusedTextBox.CaretIndex;
            LastFocusedTextBox.Text = LastFocusedTextBox.Text.Insert(caret_index, buttonContentString);
            LastFocusedTextBox.Focus();
            LastFocusedTextBox.CaretIndex = caret_index + buttonContentString.Length;
        }
    }
}

注意Button如何传递给方法而不是存储在字段中。此外,删除了all_in_one()方法的类和本地中不必要的字段。要获取网格中标签的内容(例如"1!" - 我认为这就是你所追求的,因为没有别的东西可以进入一个简单的字符串字段,并且还匹配前两个按钮的一般模式) ,在检查内容是字符串还是网格后,我们只需选择其内容并将它们连接成一个字符串。