'cellBorder' name cannot be found in the name scope

Feb 10, 2009 at 11:00 PM
Greetings,

I have been struggling with this for about a day now, and I'm not sure how to fix this issue.  Any insight would be appreciated.

I have the following DataTemplate created in the code behind - I have to create it in the code behind in order to get around a different issue (if you really want to know what that one is about, you can visit http://www.codeplex.com/wpf/Thread/View.aspx?ThreadId=45661).  Anyhow, my DataTemplate is created as follows:

1             // Assume that the following list was originally a 1D List of 30 elements, 
2             // converted dynamically to a 2D list 
3             List<List<string>tempList = new List<List<string>> { 
4                 new List<string> {"1", "2", "3", "4", "5", "6"},  
5                 new List<string> {"7", "8", "9", "10", "11", "12"},  
6                 new List<string> {"13", "14", "15", "16", "17", "18"},  
7                 new List<string> {"19", "20", "21", "22", "23", "24"}, 
8                 new List<string> {"25", "26", "27", "28", "29", "30"}, 
9             }; 
10  
11             for(int i=0; i<tempList[0].Count; i++) { 
12                 // Setup the binding in the HeatCell FEF 
13                 FrameworkElementFactory fef = new FrameworkElementFactory(typeof(HeatCell)); 
14                 fef.SetBinding(HeatCell.CenterTextProperty, new Binding("[" + i + "]")); 
15  
16                 // create the HeatCell data template 
17                 DataTemplate dataTemplate = new DataTemplate(); 
18                 dataTemplate.VisualTree = fef
19  
20                 // Add a DataGridTemplateColumn using the above data template 
21                 PlateGrid.Columns.Add(new DataGridTemplateColumn { 
22                      Header = i+1, 
23                      CellTemplate = dataTemplate
24                 }); 
25             } 
26             // set ItemsSource 
27             PlateGrid.ItemsSource = tempList
28  

My HeatCell is a XAML UserControl containing an element named 'cellBorder'
   <Rectangle x:Name="cellBorder" Fill="#FFFFFFFF"............
 
The cellBorder is used as a target (Storyboard.TargetName="cellBorder") within MouseEnter and MouseLeave storyboards.

1     <UserControl.Resources> 
2         <Storyboard x:Key="OnMouseEnter1"
3             <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="cellBorder" Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)"
4                 <SplineColorKeyFrame KeyTime="00:00:00" Value="{DynamicResource pbLightGray}"/> 
5                 <SplineColorKeyFrame KeyTime="00:00:0.25" Value="{DynamicResource pbRed}"/> 
6             </ColorAnimationUsingKeyFrames> 
7         </Storyboard> 
8         <Storyboard x:Key="OnMouseLeave1"
9             <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="cellBorder" Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)"
10                 <SplineColorKeyFrame KeyTime="00:00:00" Value="{DynamicResource pbRed}"/> 
11                 <SplineColorKeyFrame KeyTime="00:00:0.25" Value="{DynamicResource pbLightGray}"/> 
12             </ColorAnimationUsingKeyFrames> 
13         </Storyboard> 
14     </UserControl.Resources> 
15  


When I run my application code, my data grid appears just fine, showing the HeatCells laid out as desired; however, as soon as I bring my mouse on top of one of the HeatCells, I receive the error 'cellBorder' name cannot be found in the name scope 'HeatCell'.

I have read some threads about getting around this using NameScope and RegisterName, but I cannot seem to get this to work.  What I have tried is in the HeatCell's code behind, setting:
            NameScope.SetNameScope(this, new NameScope());
            this.RegisterName("cellBorder", cellBorder);

But that my problem persists.

Any thoughts as to what I may be doing wrong?

Thanks very much for your time.
E.
Feb 10, 2009 at 11:53 PM
In the HeatCell's code behind, this took care of the problem...

  private void UserControl_Loaded(object sender, RoutedEventArgs e) { 
      NameScope.SetNameScope(this, new NameScope());  
      INameScope nameScope = NameScope.GetNameScope(this); 
      NameScope.SetNameScope(cellBorder, nameScope); 
      RegisterName(cellBorder.Name, cellBorder); 
  }