我对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>
当构建项目除外错误:名称&#34; interactiveGrid&#34;在当前上下文中不存在
namespace RatesScenarios
{
public partial class MainWindow : Window
{
private void Refresh()
{
interactiveGrid.Children.Clear();
}
}
}
为什么会这样?
答案 0 :(得分:1)
如果有其他人收到此错误而没有任何迹象表明为什么正在关闭并打开.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也有类似的问题:
Calling child user-control's function
答案 1 :(得分:0)
可能这是一个愚蠢的问题,但您是否已为InteractiveGrid声明了公共构造函数? 你在MainWindow构造函数中调用InitializeComponent()方法吗?
答案 2 :(得分:0)
现有代码,不构建
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();
}
//..
}