I have a set of controls with attached commands and logic that are constantly reused in the same way. I decided to create a user control that holds all the common controls and logic.
However I also need the control to be able to hold content that can be named. I tried the following:
<UserControl.ContentTemplate> <DataTemplate> <Button>a reused button</Button> <ContentPresenter Content="{TemplateBinding Content}"/> <Button>a reused button</Button> </DataTemplate> </UserControl.ContentTemplate> <UserControl.ContentTemplate> <DataTemplate> <Button>a reused button</Button> <ContentPresenter Content="{TemplateBinding Content}"/> <Button>a reused button</Button> </DataTemplate> </UserControl.ContentTemplate> However it seems any content placed inside the user control cannot be named. For example if I use the control in the following way:
<lib:UserControl1> <Button Name="buttonName">content</Button> </lib:UserControl1> <lib:UserControl1> <Button Name="buttonName">content</Button> </lib:UserControl1> I receive the following error:
Cannot set Name attribute value 'buttonName' on element 'Button'. 'Button' is under the scope of element 'UserControl1', which already had a name registered when it was defined in another scope.
If I remove the buttonName, then it compiles, however I need to be able to name the content. How can I achieve this?