Answered Cannot set Name attribute value

  • Saturday, September 09, 2006 5:39 PM
     
     

    I have a user control.  I have defined a ContentTempalte for it:

    <UserControl x:Class="MyUc"...>
     <UserControl.ContentTemplate>
      ...
      <StackPanel>
       <ContentPresenter Content="{TemplateBinding Content}"/>
      </StackPanel>
     </UserControl.ContentTemplate>
    </UserControl>

    Now In a window I can do things like:
    <Page...>
     <src:MyUC>
      <ListView>
      </ListView>
     </src:MyUC>
    </Page>

    This works great, I can see the listview.

    However if I try to name the listView: <Listview x:name="MyListView"/>

    The compiler throws an error:

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

    Any idea what I'm doing wrong?

    Thanks in advance.

    Thanks
    Houman

    This is very similar to my problem, but to be honest I don't understand the solution :(

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=487058&SiteID=1

Answers

  • Tuesday, August 28, 2007 2:45 AM
    Moderator
     
     Answered

    In order to support namable content, look to customcontrols, not user controls.

    I've blogged a bit about this here: http://rrelyea.spaces.live.com/Blog/cns!167AD7A5AB58D5FE!2130.entry

     

    Thanks, Rob Relyea | Program Manager, WPF & Xaml Language Team
    robrelyea.com | /blog | /wpf | /xaml

     

     

All Replies

  • Saturday, September 09, 2006 9:04 PM
     
     

    OK, here is how I got around it...it works but feels like a hack to me.  Here was the setup:

    <UserControl x:Class="MyUc"...>
     <UserControl.ContentTemplate>
      ...
      <StackPanel>
       <ContentPresenter Content="{TemplateBinding Content}"/>
      </StackPanel>
     </UserControl.ContentTemplate>
    </UserControl>

    Now In a page.xaml I can do things like:
    <Page...>
     <src:MyUC>
      <ListView x:Name="lv">
      </ListView>
     </src:MyUC>
    </Page>

    I changed the page.xaml to:

    <Page...>
     <src:MyUC>
       <src:MyUC.Resources>
              <ListView x:key="lv">
              </ListView>
       </src:MyUC.Resources>

       <!--HACK-->
       <StackPanel>
          <ContentPresenter Content={StaticResource lv}/>
       </StackPanel>

       <Button Content="clickMe" IsEnabled={Binding Source={StaticResource lv}, Path=IsEnabled}/>

     </src:MyUC>
    </Page>

    Now I can access the lv or bind to it by looking into the dictionary (check out the button's isEnabled Property).

    So far it works but it doesn't give me a good feeeling.

    Is there a better way?

    Thanks

    Houman

  • Tuesday, July 17, 2007 9:48 PM
     
     

    I'm running into the same problem for a section control I'm working on. I image this is a problem for anyone creating user controls meant to contain other controls.

     

    Did you happen to find a better solution for this?

     

    There has to be a better way because I'm sure I've seen controls that hold generic ui element content without this limitation on use.

  • Friday, July 20, 2007 5:28 PM
     
     
    Just a quick additional note for anyone interested - I was unable to find anything other the the workaround suggested above. Ultimately I ended up creating a CustomControl derived from a control designed to hold other elements (I used Expander). This allowed me to have a control with named content (set in the XAML by the end user of the control). If anyone knows of a better workaround for allowing named items in a UserControl though it would still be nice to know.
  • Monday, August 27, 2007 9:25 AM
     
     
    I had this issue today and found a nice way around it.

    I've posted the resolution on my blog: http://blog.bluecog.co.nz/archives/2007/08/27/wpf-cannot-set-name-attribute/

    Hope that helps,

    John-Daniel Trask
    http://blog.bluecog.co.nz
  • Tuesday, August 28, 2007 2:45 AM
    Moderator
     
     Answered

    In order to support namable content, look to customcontrols, not user controls.

    I've blogged a bit about this here: http://rrelyea.spaces.live.com/Blog/cns!167AD7A5AB58D5FE!2130.entry

     

    Thanks, Rob Relyea | Program Manager, WPF & Xaml Language Team
    robrelyea.com | /blog | /wpf | /xaml

     

     

  • Tuesday, February 23, 2010 10:00 PM
     
     
  • Tuesday, March 23, 2010 3:50 PM
     
      Has Code

    an other option is to name the container and then get the child from the container. In my scenario I have a UcContainer that does some layout with nested in it an other Uc (NestedUc) that has the logic. There are actually 4 containers in the actual code that create an acordeon setup but they are left out for simplicity.

    This is pretty much a hack, but the code is consise.

    In the contructor:

     

    InitializeComponent();
    State = new Data(); 
    this.DataContext = State; 
    var NestedUc= UcContainer.Children[0] as MyNestedUc;
    NestedUc.State = State;

    If you want to you can even hide it behind a property.

    • Edited by BasHamer Tuesday, March 23, 2010 3:52 PM formatting
    •