Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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# Can';t访问.xaml.cs中文本框的文本_C#_Wpf_Xaml_Textbox_Textblock - Fatal编程技术网

C# Can';t访问.xaml.cs中文本框的文本

C# Can';t访问.xaml.cs中文本框的文本,c#,wpf,xaml,textbox,textblock,C#,Wpf,Xaml,Textbox,Textblock,这很奇怪。我在.xaml中有一个文本块(称之为ErrorMessage_Textblock),当我试图在.xaml.cs中访问和更改它的文本时,它向我抛出一个错误,说“当前上下文中不存在名称'ErrorMessage_Textblock' 基本上,当程序运行时,ErrorMessage_Textblock假定为空。当用户单击开始按钮时,我在.xaml.cs中的代码将检查用户是否在文本框中填写了所有必要的信息。如果缺少一些信息,它将向ErrorMessage_Textblock传递一个字符串——比

这很奇怪。我在.xaml中有一个文本块(称之为ErrorMessage_Textblock),当我试图在.xaml.cs中访问和更改它的文本时,它向我抛出一个错误,说“当前上下文中不存在名称'ErrorMessage_Textblock'

基本上,当程序运行时,ErrorMessage_Textblock假定为空。当用户单击开始按钮时,我在.xaml.cs中的代码将检查用户是否在文本框中填写了所有必要的信息。如果缺少一些信息,它将向ErrorMessage_Textblock传递一个字符串——比如“请输入保存文件的位置”

SideMenuControl.xaml:

<UserControl x:Class="Fasetto.Word.SideMenuControl"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup- compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Fasetto.Word"
         xmlns:core="clr- 
namespace:Fasetto.Word.Core;assembly=Fasetto.Word.Core"
         mc:Ignorable="d" d:DesignWidth="900" d:DesignHeight="1000" 
Background="#FF2D2D30">



<UserControl.Template>
    <ControlTemplate TargetType="UserControl">
        <Border>

//some code ...

<Button
 Click="StartButton"
 Content="Start" FontSize="30" Padding="1,1,1,1" Foreground="Lime" 
 BorderBrush="Red"
/>


<TextBlock x:Name="ErrorMessage_Textblock" Foreground="OrangeRed"           
  FontFamily="/VIL_GUI_V5.0;component/Fonts/#Lato Light" 
  Margin="50,10,50,510" FontSize="20"
/>

我找到了自己的答案。您已经可以访问按钮,因此可以找到它所属的网格。然后,您可以找到TextBlock。唯一的问题是TextBlock和按钮属于同一个网格

有关完整的指南和代码,请单击此链接:


至少您需要
ErrorMessage\u Textblock.Text=…
您不能直接访问在代码隐藏中的
ControlTemplate
中定义的控件。你为什么要首先使用
ControlTemplate
?@mm8我在网上找到了一个项目,这就是最初的开发人员是如何实现的。在代码隐藏中是否有访问ControlTemplate控件的解决方案?
 public void StartButton(object sender, RoutedEventArgs e) {


        if (Fasetto.Word.Core.IoC.Settings.Monaco_Report_Type.EditedText 
== null || Fasetto.Word.Core.IoC.Settings.Monaco_Report_Type.EditedText == "")
        {
            //do something
            ErrorMessage_Textblock = "please enter Report Type (Monaco)";

        }