如何从按钮UI获取与普通按钮不同的内容

时间:2014-03-05 09:53:18

标签: windows-phone-8 winrt-xaml

问题:

这不适用于以下Button UI的代码隐藏:

string strButtonText = btnConfirm.Content.ToString();

非常感谢您的帮助。 TIA


<Button x:Name="btnConfirm" Background="Crimson" Foreground="White" FontSize="21" HorizontalAlignment="Left" Margin="662,640,0,0" VerticalAlignment="Top" Height="93" Width="166" Click="btnConfirm_Click">

  <TextBlock Text="Begin" TextWrapping="Wrap" Width="138" Height="61" />

</Button>

The code only work for normal buttun UI:

<Button x:Name="btnConfirm" content="Begin" Background="Crimson" Foreground="White" FontSize="21" HorizontalAlignment="Left" Margin="662,640,0,0" VerticalAlignment="Top" Height="93" Width="166" Click="btnConfirm_Click />


Update : 

How to you set the text back to the button UI in code ??


if (strButtonText.Contains("Begin Date") || strButtonText.Contains("Begin"))
 {

       ???
     btnConfirm.Content = "End Date";


   }
   else
   {
         ??
        btnConfirm.Content = "Begin";
  }


2 个答案:

答案 0 :(得分:0)

好的,同样适用于win rt。

尝试这样:

 //Getting
 string s = (btnConfirm.Content as Windows.UI.Xaml.Controls.TextBlock).Text;
 //Setting
 (btnConfirm.Content as as Windows.UI.Xaml.Controls.TextBlock).Text = "Hello";  

答案 1 :(得分:0)

试试这个

  <Button  x:Name="btnConfirm" Tag="{Binding ElementName=content,Path=Text}" Background="Crimson" Foreground="White" FontSize="21" HorizontalAlignment="Left" Margin="662,640,0,0" VerticalAlignment="Top" Height="93" Width="166" Click="btnConfirm_Click">            
      <TextBlock x:Name="content" Text="begin" TextWrapping="Wrap" Width="138" Height="61" />            
  </Button>

并点击事件写入

  private void btnConfirm_Click(object sender, RoutedEventArgs e)
    {
        string strButtonText = btnConfirm.Tag.ToString();
    }