简体   繁体   English

C#XAML - 获取XAML内容属性(contentControl.Content)

[英]C# XAML - Getting the XAML Content property (contentControl.Content)

So I have my AML keypad as such : 所以我有我的AML键盘:

    <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 run when each button is clicked but I cannot work out how to use the Content from the XAML. 现在我想编写一个小帮助程序方法,以便在单击每个按钮时运行但我无法弄清楚如何使用XAML中的内容。

for example this works for button 1 : 例如,这适用于按钮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 being pressed? 辅助方法使用btnOne.Content但是如何让它使用任何按下按钮的内容? So I can just add the helper methosto each button? 所以我可以添加助手methosto每个按钮?

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

You can use the sender variable to determine the button clicked: 您可以使用sender变量来确定单击的按钮:

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

add btn_Click to the Click event of each button (and replace the ones you have already) btn_Click添加到每个按钮的Click事件中(并替换已经存在的那个)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM