I'm trying to put a textblock in a grid element, but it doesn't display in the debug mode. What do I do wrong? Maybe it's caused that I manipulate the window directly by my C# code?
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsof开发者_如何学Got.com/winfx/2006/xaml"
Title="MainWindow" Height="400" Width="500" ResizeMode="NoResize">
<Window.Background>
<ImageBrush ImageSource="/WpfApplication2;component/Images/Grass0118_22_S.jpg"></ImageBrush>
</Window.Background>
<Grid>
<TextBlock Margin="10,10,0,0" Foreground="White" FontWeight="Bold">Życia:</TextBlock>
<TextBlock Margin="50,10,0,0" Foreground="White" Text="{Binding Text, ElementName=points}"></TextBlock>
</Grid>
</Window>
Try this
<Grid>
<Stackpanel Orientation = "Horizontal">
<TextBlock Margin="10,10,0,0" Foreground="White" FontWeight="Bold">Życia:</TextBlock>
<TextBlock Margin="50,10,0,0" Foreground="White" Text="{Binding Text, ElementName=points}"></TextBlock>
</Stackpanel>
</Grid>
I've already solved my issue. I had defined a canvas tag in my C# code and it overwrote the XAML changes. I replaced the XAML code to the following
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="400" Width="500" ResizeMode="NoResize">
<Window.Background>
<ImageBrush ImageSource="/WpfApplication2;component/Images/Grass0118_22_S.jpg"></ImageBrush>
</Window.Background>
<Canvas>
<Grid>
<TextBlock Margin="10,10,0,0" Foreground="White" FontWeight="Bold">Życia:</TextBlock>
<TextBlock Margin="50,10,0,0" Foreground="White" Text="{Binding Text, ElementName=points}"></TextBlock>
</Grid>
<Canvas Name="mycanvas"></Canvas>
</Canvas>
</Window>
and removed the line creating the canvas element from my C# code and everything works now. Thanks!
精彩评论