Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XAML更改无效_C#_Wpf_Xaml - Fatal编程技术网

C# XAML更改无效

C# XAML更改无效,c#,wpf,xaml,C#,Wpf,Xaml,我试图在网格元素中放置textblock,但它不会在调试模式下显示。我做错了什么?可能是因为我直接用C代码操纵窗口 Życia: 试试这个 <Grid> <Stackpanel Orientation = "Horizontal"> <TextBlock Margin="10,10,0,0" Foreground="White" FontWeight="Bold">Życia:</TextBlock> &l

我试图在网格元素中放置textblock,但它不会在调试模式下显示。我做错了什么?可能是因为我直接用C代码操纵窗口


Życia:
试试这个

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

Życia:

我已经解决了我的问题。 我在C#代码中定义了一个canvas标记,它重写了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>

Życia:
从我的C#代码中删除了创建canvas元素的行,现在一切都正常了。
谢谢

你能提供相关代码吗?关于你的问题的一些具体细节?上面附加的代码。如果你在代码隐藏中操作窗口,为什么不也添加它?因为使用XAML比直接使用C#代码更容易,对吧?我的意思是你应该发布与问题相关的任何代码。不要把答案贴在问题里面,把它们作为实际答案贴出来,然后接受它们。这是一样的。我假设由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>
    <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>