2014-10-06 101 views
1

我对wpf有一些疑问。 有一个我的课Wpf名称“interactiveGrid”在当前上下文中不存在

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(); 
     } 
    } 
} 

为什么发生?

+0

您是否尝试将公共添加到控件的定义中?公共类InteractiveGrid – rauland 2014-10-06 06:50:54

+0

是的,“公共类InteractiveGrid”也不起作用。定义“类InteractiveGrid”必须工作,因为它在单个程序集中声明 – Andre 2014-10-06 06:54:38

+1

您是否尝试过x:Name =“interactiveGrid”而不是Name =“interactiveGrid”? – rauland 2014-10-06 07:10:09

回答

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

0

也许这是一个愚蠢的问题,但你有没有声明InteractiveGrid的公共构造函数? 你在MainWindow构造函数中调用InitializeComponent()方法吗?

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(); 
    } 
//.. 
} 
+0

这是你的解决方案吗?如果是这样,请添加一些关于您为解决问题而采取的步骤的评论。如果没有,你应该在下次编辑你的问题。 – Sjeijoet 2014-10-06 07:12:23

相关问题