Wpf名称“;interactiveGrid”;在当前上下文中不存在

本文关键字:上下文 不存在 名称 interactiveGrid Wpf | 更新日期: 2023-09-27 18:25:42

我对wpf有一些问题。有一个我班的

using System;
using System.Collections.Generic;
using System.Windows.Controls;
namespace RatesScenarios.Controls
{
    class InteractiveGrid : Grid, IDisposable
    {
    //...
    }
}

当我将其添加到xaml:时

<Window x:Class="RatesScenarios.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:RatesScenarios.Controls"
        Title="RatesScenarios" MinHeight="400" Width="700" Background="SteelBlue" SizeToContent="Manual">

低于

<Grid Background="White">
<Border BorderBrush="#CCCCCC" BorderThickness="1" Margin="7,10,7,10" VerticalAlignment="Top" HorizontalAlignment="Center">
<controls:InteractiveGrid Name="interactiveGrid" ShowGridLines="True" VerticalAlignment="Top" HorizontalAlignment="Center">
</controls:InteractiveGrid>
</Border>
</Grid>

当生成项目异常时:名称"interactiveGrid"在当前上下文中不存在

namespace RatesScenarios
{
public partial class MainWindow : Window
{
      private void Refresh()
      {
         interactiveGrid.Children.Clear();
      }
   }
}

为什么会这样?

Wpf名称“;interactiveGrid”;在当前上下文中不存在

如果其他人在没有任何指示为什么会发生此错误的情况下收到此错误,则关闭和打开.XAML文件会提示以下内容:

Error 2 Because 'MS.Internal.Design.Metadata.ReflectionTypeNode' is   
implemented in the same assembly, you must set the x:Name   
attribute rather than the  
MS.Internal.Design.Metadata.ReflectionPropertyNode attribute.

SO上也有类似的问题:
正在调用子用户控件';s函数

这可能是一个愚蠢的问题,但您已经为InteractiveGrid声明了公共构造函数吗?是否在MainWindow构造函数中调用InitializeComponent()方法?

存在现有代码,不构建

using System;
using System.Collections.Generic;
using System.Windows.Controls;
namespace RatesScenarios.Controls
{
    public class InteractiveGrid : Grid, IDisposable
    {
    #region IDisposable Members
        public void Dispose()
        {
        }
    #endregion
        private DataCell selectedCell;
        public DataCell SelectedCell
        {
            get { return selectedCell; }
        }
        public InteractiveGrid()
        {
        }
        public void Build(List<string> columnsHeaders, List<string> rowsHeaders, List<DataCell> dataCells)
        {
        }
        public void SelectCell(DataCell dataCell)
        {
            selectedCell = dataCell;
        }
    }
}

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
//..
}