2013-05-03 40 views
4

我想更改Windows手机上TextBlock的背景颜色。现在我只有一个没有空格的文本块。这种效果,我这段代码有:windows phone上的背景文字

<StackPanel Orientation="Horizontal" Background="{Binding Color}"> 
    <TextBlock Text="{Binding Name}" Margin="12,0,0,0"></TextBlock> 
</StackPanel> 

回答

11

TextBlock的没有对自己的背景属性。你必须建立一个背景网格或画布或边框或矩形来填满它。

<Grid Width="300" Height="100" Background="Blue"> 
      <TextBlock Name="MyTextBlock" Text="Hello World!" Foreground="Black" /> 
</Grid> 

而不是网格,你可以做一个矩形或边框。

+0

为了有一个总是具有相同的“近帧空间”动态字符串,从网格中删除的宽度/高度,并添加保证金文本块。 – XAMeLi 2013-05-04 10:03:16

1

你也可以改变背景颜色上getFocus事件像

private void TextBox_GotFocus(object sender, RoutedEventArgs e) 
{ 
    (sender as TextBox).Background = new SolidColorBrush(Colors.Red); 
}