vbtricks

Salut,

in my WPF application I created a UserControl, that works the way I want it to.
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
<UserControl x:Class="TestProject.PopupMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.ContentTemplate>
<DataTemplate>
<Grid>
<Canvas Background="Black" Opacity="0.7" />
<Grid Margin="50" Background="DarkGray">
<ContentPresenter Content="{TemplateBinding Content}" Margin="5" />
</Grid>
</Grid>
</DataTemplate>
</UserControl.ContentTemplate>
</UserControl>

The only problem is, when I try to use multiple instances of it on a single form and to name them:

code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
<UserControl x:Class="TestProject.BackgroundChangerInternalControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:TestProject;assembly=TestProject">
<Grid>
<!-- ... -->
<controlsStick out tongueopupMenu Name="ModifyPopupMenu" Grid.Row="0" Grid.RowSpan="2">
<StackPanel Orientation="Vertical">
<TextBlock Style="{StaticResource PopupMenuCaption}">Modify</TextBlock>
<Button Name="StretchButton" Click="SizeModeButton_Click">Stretch</Button>
<Button Name="DontEnlargeButton" Click="SizeModeButton_Click">Don't enlarge</Button>
<Button Name="NoResizeButton" Click="SizeModeButton_Click">No resize</Button>
<Button Name="CancelModifyButton" Click="CancelButton_Click" Margin="">Cancel</Button>
</StackPanel>
</controlsStick out tongueopupMenu>

<controlsStick out tongueopupMenu Name="ExportPopupMenu" Grid.Row="0" Grid.RowSpan="2">
<StackPanel Orientation="Vertical">
<TextBlock Style="{StaticResource PopupMenuCaption}">Export</TextBlock>
<ComboBox Name="ResolutionsComboBox" />
<Button Name="ExportDoButton" Grid.Row="1" Click="ExportDoButton_Click">Export</Button>
<Button Name="CancelExportButton" Grid.Row="2" Click="CancelButton_Click">Cancel</Button>
</StackPanel>
</controlsStick out tongueopupMenu>
<!-- ... -->
</Grid>
</UserControl>


Stefan

I'm getting the following error:
Quote:

Error 2 'TestProject.PopupMenu.Name' property has already been set and can be set only once. Line 41 Position 35.

What am I doing wrong, how can I fix it


Thanks in advance,

Stefan


Re: Windows Presentation Foundation (WPF) Multiple instances of a UserControl, setting Name property

Yi-Lun Luo - MSFT

Hello, so you want to create a Control whose Content can be customized I think in your case, you should create a CustomControl inherited from ContentControl, not a UserControl. You can define a ControlTemplate for your Control, not a ContentTemplate. Try something like this:

<Style TargetType="{x:Type local:PopupMenu}">

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="{x:Type local:PopupMenu}">

<Grid>

<Canvas Background="Black" Opacity="0.7" />

<Grid Margin="50" Background="DarkGray">

<ContentPresenter Content="{TemplateBinding Content}" Margin="5" />

</Grid>

</Grid>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

Also, use x:Name instead of Name since this Control is in the same assembly.






Re: Windows Presentation Foundation (WPF) Multiple instances of a UserControl, setting Name property

vbtricks

Salut,

thanks for your reply. Tried to do the changes you proposed and well, it works, till I try to add two buttons to the content and name them. I'm getting the same error again.

Have attached the sample project: PopupMenu.zip

Would be very kind of you, if you could have a look...


Thanks in advance,

Stefan