C # XAML - Getting XAML content property (contentControl.Content)

So I have an AML keyboard as such:                                                                           

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <TextBox Name="btnTotal" Width="280" Height="60" Grid.ColumnSpan="3" Grid.Row="0" Margin="10,10,10,10" Background="#302F37" Foreground="AntiqueWhite" FontSize="35"></TextBox>
    <Button x:Name="btnZzero" Content="0" Width="80" Height="60" Grid.Column="0" Grid.Row="4" Margin="5,5,5,5" Background="#302F37" Foreground="White" Focusable="False"  Click="btnZzero_Click"></Button>
    <Button x:Name="btnOk" Content="OK" Width="80" Height="60" Grid.Column="1" Grid.Row="4" Margin="5,5,5,5" Click="btnOk_Click" Background="#FF8FC377" Focusable="False"></Button>
    <Button x:Name="btnCancel" Content="Cancel" Width="80" Height="60" Grid.Column="2" Grid.Row="4" Margin="5,5,5,5" Click="cancel_Click" BorderBrush="Black" Background="#FFD64D4D" Focusable="False"></Button>
    <Button x:Name="btnOne" Content="1" Width="80" Height="60" Grid.Column="0" Grid.Row="3" Margin="14,6,0,6" Focusable="False" Background="#302F37" Foreground="White" HorizontalAlignment="Left" Click="btnOne_Click"></Button>
    <Button x:Name="btnTwo" Content="2" Width="80" Height="60" Grid.Column="1" Grid.Row="3" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnTwo_Click"/>
    <Button x:Name="btnThree" Content="3" Width="80" Height="60" Grid.Column="2" Grid.Row="3" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnThree_Click"/>
    <Button x:Name="btnFour" Content="4" Width="80" Height="60" Grid.Column="0" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnFour_Click"></Button>
    <Button x:Name="btnFive" Content="5" Width="80" Height="60" Grid.Column="1" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnFive_Click"></Button>
    <Button x:Name="btnSix" Content="6" Width="80" Height="60" Grid.Column="2" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnSix_Click"></Button>
    <Button x:Name="btnSeven" Content="7" Width="80" Height="60" Grid.Column="0" Grid.Row="1" Margin="12,5,9,6" Focusable="False" Background="#302F37" Foreground="White" Click="btnSeven_Click"></Button>
    <Button x:Name="btnEight" Content="8" Width="80" Height="60" Grid.Column="1" Grid.Row="1" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnEight_Click"></Button>
    <Button x:Name="btnNine" Content="9" Width="80" Height="60" Grid.Column="2" Grid.Row="1" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnNine_Click"></Button>

      

Now I want to write a small helper method to trigger on each button click, but I can't figure out how to use the content from XAML.

for example this works for button 1:

private void btnOne_Click(object sender, RoutedEventArgs e)
    {

        addButtonNumberToResult();
     }

      

And the helper method uses btnOne.Content, but how do I get it to use the content from any button clicked? So I can just add a helper method to each button?

    private void addButtonNumberToResult()  //helper method 
    {
        if (btnClicked == true)
        {
            btnTotal.Text = "";
            btnClicked = false;
        }
        QuantityResult = QuantityResult += btnOne.Content;
        btnTotal.Text = QuantityResult;
    }

      

+3


source to share


1 answer


You can use a variable sender

to define the pressed button:

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

      



add btn_Click

to Click

each button event (and replace the ones you already have)

private void addButtonNumberToResult(Button btn)  //helper method 
{
    if (btnClicked == true)
    {
        btnTotal.Text = "";
        btnClicked = false;
    }
    QuantityResult = QuantityResult += btn.Content;
    btnTotal.Text = QuantityResult;
}

      

+1


source







All Articles