none
How to set x:Name in C#

    Question

  • Hi,

     

    This is my xbap code behind for creating a GeometryModel3D on Page loaded:

     

    void PageOnLoaded(object sender, RoutedEventArgs args)

    {

      GeometryModel3D model3D = new GeometryModel3D();

      model3D.Geometry = (Geometry3D)this.Resources["sealevel"];

      DiffuseMaterial material = new DiffuseMaterial();

      ImageBrush brush = new ImageBrush();

      brush.ImageSource = (ImageSource)this.Resources["encImg"];

      material.Brush = brush;

     

      //set x:Name for DiffuseMaterial

      NameScope pageScope = new NameScope();

      NameScope.SetNameScope(this, pageScope);

      pageScope.RegisterName("encMat4", material);

     

      model3D.Material = material;

      Model3DGroup gp = main.Content as Model3DGroup;

      gp.Children.Add(model3D);

    }

     

    However, I need to change the material with a reset button :

    void ButtonResetLayer(object sender, RoutedEventArgs args)

    {

      encMat4.ClearValue(DiffuseMaterial.BrushProperty);

                     .

                     .

    }

     

    This error occurs on build:

       "The name 'encMat4' does not exist in current context"

     

    How do I correctly set the x:Name='encMat4' in C# code so that I can access the DiffuseMaterial by x:Name?

     

    thanks

    Friday, July 06, 2007 5:06 PM

Answers

  • Calling RegisterName programatically does not create a variable in your code-behind, that's a feature of XAML compilation. So, if you're going to do it programatically, in your ButtonResetLayer you need to do: 

    Code Snippet

    NameScope pageScope = NameScope.GetNameScope(this);

    DiffuseMaterial material = (DiffuseMaterial)pageScope.FindName("encMat4");

     

    material.ClearValue(DiffuseMaterial.BrushProperty);

     

     HTH,
    Drew

    Friday, July 06, 2007 6:12 PM
  •  

    Thanks, that helped.

     

    After a couple of modifications I was able to get this to work. I believe I need to hold the nameScope reference between the RegisterName and the FindName,

     

    public partial class Page1 : System.Windows.Controls.Page

    {

         private NameScope pageScope = new NameScope();

                                    .

                                    .

     

      void PageOnLoaded(object sender, RoutedEventArgs args)

      {

                                    .

                                    .

          NameScope.SetNameScope(this, pageScope);

          pageScope.RegisterName("encMat2", material);

                                    .

                                    .

      }

     

                                    .

                                    .

     

      void ButtonResetLayer(object sender, RoutedEventArgs args)

      {

        object obj = pageScope.FindName("encMat2");

        if (obj is DiffuseMaterial)

          {

            DiffuseMaterial material = obj as DiffuseMaterial;

            material.ClearValue(DiffuseMaterial.BrushProperty);

          }

      }

    Friday, July 06, 2007 8:08 PM

All replies

  • Calling RegisterName programatically does not create a variable in your code-behind, that's a feature of XAML compilation. So, if you're going to do it programatically, in your ButtonResetLayer you need to do: 

    Code Snippet

    NameScope pageScope = NameScope.GetNameScope(this);

    DiffuseMaterial material = (DiffuseMaterial)pageScope.FindName("encMat4");

     

    material.ClearValue(DiffuseMaterial.BrushProperty);

     

     HTH,
    Drew

    Friday, July 06, 2007 6:12 PM
  •  

    Thanks, that helped.

     

    After a couple of modifications I was able to get this to work. I believe I need to hold the nameScope reference between the RegisterName and the FindName,

     

    public partial class Page1 : System.Windows.Controls.Page

    {

         private NameScope pageScope = new NameScope();

                                    .

                                    .

     

      void PageOnLoaded(object sender, RoutedEventArgs args)

      {

                                    .

                                    .

          NameScope.SetNameScope(this, pageScope);

          pageScope.RegisterName("encMat2", material);

                                    .

                                    .

      }

     

                                    .

                                    .

     

      void ButtonResetLayer(object sender, RoutedEventArgs args)

      {

        object obj = pageScope.FindName("encMat2");

        if (obj is DiffuseMaterial)

          {

            DiffuseMaterial material = obj as DiffuseMaterial;

            material.ClearValue(DiffuseMaterial.BrushProperty);

          }

      }

    Friday, July 06, 2007 8:08 PM