2011-06-05 67 views
0

我试图将一个文本块放入网格元素,但它不显示在调试模式下。我做错了什么?也许这是我的C#代码直接操纵窗口的原因?XAML更改无效

<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> 
    <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> 
+2

你能提供相关的代码吗?还有一些关于你的问题的具体细节? – 2011-06-05 15:41:20

+0

上面附上的代码。 – deem 2011-06-05 15:52:18

+0

如果你在后面的代码中操作窗口,为什么不添加它? – 2011-06-05 16:03:46

回答

0

我已经解决了我的问题。 我在C#代码中定义了一个画布标签,并覆盖了XAML更改。我更换了XAML代码如下

<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> 

和删除线创建从我的C#代码中的canvas元素和一切正常了。 谢谢!

0

试试这个

<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> 
+0

这是一样的。我想由C#代码设置的元素会覆盖XAML项目。你对此有何看法? – deem 2011-06-05 15:55:19

+0

请发布您的'C#'代码 – 2011-06-05 15:56:10