none
How to create a WPF UserControl with Named content? RRS feed

  • Question

  • hi,

    i cannot set name for grid:

            <ctrl:TestControl>
                <Grid x:Name="myGrid" >
                    <Button  Content="Button" Height="23" HorizontalAlignment="Left"
     Margin="10,10,0,0" VerticalAlignment="Top" Width="75" /> </Grid> </ctrl:TestControl>

     

    error:

    Cannot set Name attribute value 'myGrid' on element 'Grid'. 'Grid' is under the scope of element 'TestControl', which already had a name registered when it was defined in another scope.

    im am test this site and not worked:

    http://blog.bluecog.co.nz/archives/2007/08/27/wpf-cannot-set-name-attribute/

     

    http://stackoverflow.com/questions/751325/how-to-create-a-wpf-usercontrol-with-named-content/757810#757810


    Wednesday, November 2, 2011 2:45 PM

Answers

  • Hi Ali Visual Studio,

    Firstly, the blog you metioned above:

    http://blog.bluecog.co.nz/archives/2007/08/27/wpf-cannot-set-name-attribute/

    could work for your case, however, you could not create your UserControl with normal(Xaml and .CS), you have to create a normal class which inherit from UserControl, for example:

    public partial class test : UserControl
    {
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
    
            Border border = new Border();
            border.VerticalAlignment = VerticalAlignment.Stretch;
            border.HorizontalAlignment = HorizontalAlignment.Stretch;
    
            Grid grid = new Grid();
    
            ContentPresenter content = new ContentPresenter();
            content.Content = Content;
    
            grid.Children.Add(content);
            border.Child = grid;
    
            Content = border;
        }
    }
    

    That means you will drop all ui design, you have to do this work in .cs file, then you could achieve this goal:

    <ctrl:TestControl>
                <Grid x:Name="myGrid" >
                    <Button  Content="Button" Height="23" HorizontalAlignment="Left"
     Margin="10,10,0,0" VerticalAlignment="Top" Width="75" />
                </Grid>
            </ctrl:TestControl>

    On the other hand, if you could try to create a CustomControl instead of UserControl, and then you could use inner Name attribute.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, November 7, 2011 8:51 AM
    Moderator

All replies

  • Hi for the setting name on child control you need to use the custom control, name for the child control will not work with User Control.

    If you still want to use User Control you can read more at Click Here  


    Regards
    Nayan Paregi (MCTS)
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, November 3, 2011 6:13 AM
  • Hi,

    the error message seems to mean that the x:Name attribute of TestControl has already been set. This could be in the UserControl's defining XAML file. So, you could check that.

    Further, if you want to add Content to a UserControl in XAML, you need to set the System.Windows.Markup.ContentPropertyAttribute in the UserControl's code behind file, so the XAML parser knows where the content should go.


    http://wpfglue.wordpress.com
    Thursday, November 3, 2011 6:52 AM
  • Hi Ali Visual Studio,

    Firstly, the blog you metioned above:

    http://blog.bluecog.co.nz/archives/2007/08/27/wpf-cannot-set-name-attribute/

    could work for your case, however, you could not create your UserControl with normal(Xaml and .CS), you have to create a normal class which inherit from UserControl, for example:

    public partial class test : UserControl
    {
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
    
            Border border = new Border();
            border.VerticalAlignment = VerticalAlignment.Stretch;
            border.HorizontalAlignment = HorizontalAlignment.Stretch;
    
            Grid grid = new Grid();
    
            ContentPresenter content = new ContentPresenter();
            content.Content = Content;
    
            grid.Children.Add(content);
            border.Child = grid;
    
            Content = border;
        }
    }
    

    That means you will drop all ui design, you have to do this work in .cs file, then you could achieve this goal:

    <ctrl:TestControl>
                <Grid x:Name="myGrid" >
                    <Button  Content="Button" Height="23" HorizontalAlignment="Left"
     Margin="10,10,0,0" VerticalAlignment="Top" Width="75" />
                </Grid>
            </ctrl:TestControl>

    On the other hand, if you could try to create a CustomControl instead of UserControl, and then you could use inner Name attribute.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, November 7, 2011 8:51 AM
    Moderator
  • hi,

    but i am test and not working,

    please see me one example.

    Monday, November 14, 2011 3:35 PM
  • Hi,

    Just copy the code above, and put test class into your .cs file, and then apply this class in your Xaml, it will work.

     

     


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, November 15, 2011 3:25 AM
    Moderator