Skip to main content
edited tags
Link
brunnerh
  • 188k
  • 30
  • 362
  • 434
Syntax highlighting
Source Link
brunnerh
  • 188k
  • 30
  • 362
  • 434

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?

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> 

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> 

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?

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> 

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> 

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?

Source Link
Ryan
  • 1.7k
  • 2
  • 14
  • 14

How to create a WPF UserControl with NAMED content

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> 

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> 

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?