Grab value from a button inside 'Contentcontrol'

Asked

Viewed 241 times

1

I have the following code on XAML:

<Button Grid.Column="0" Grid.Row="1" x:Name="btnSete" Click="btn_Click">
    <ContentControl>
        <Viewbox Margin="3">
            <TextBlock Text="7"/>
        </Viewbox>
    </ContentControl>           
</Button>

and in code Behind:

private void btn_Click(object sender, RoutedEventArgs e)
{
    Button btn = (Button)sender;
    //txbDisplay.Text = btn.ContentStringFormat.ToString();
}

Doubt now is, how do I stop at the Click take the content that is within:

<ContentControl>
    <Viewbox Margin="3">
        <TextBlock Text="7"/>
    </Viewbox>
</ContentControl>

button?

I want to show in the txbDisplay the value that is in <TextBlock Text="7"/>.

1 answer

1


I was able to solve it this way:

I removed the <ContentControl>

<Button Grid.Column="1" Grid.Row="2" x:Name="btnCinco" Click="btn_Click">
    <Viewbox Margin="3">
        <TextBlock Text="5"/>
    </Viewbox>
</Button>

and in the event I did so:

private void btn_Click(object sender, RoutedEventArgs e)
{
    Button btn = (Button)sender;

    var viewBox = (Viewbox)btn.Content;
    var txtBlock = (TextBlock)viewBox.Child;
    var text = txtBlock.Text;

    txbDisplay.Text += text;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.