0
<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>

设计会是这样的

结果

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;
        }
    }
}

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

4

2 回答 2

1

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

var btn_c = (Button)sender;
Grid grid = (Grid)btn_c.Content;
Label label1 = (Label)grid.Children[0];
string name1 = label1.Name;
于 2012-07-04T03:55:17.393 回答
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!"- 我假设这是您所追求的,因为没有其他东西可以进入一个简单的字符串字段,并且也匹配您前两个按钮的一般模式),我们只需选择它们在检查内容是字符串还是网格之后,将它们连接成一个字符串。

于 2012-07-04T03:55:43.990 回答