2

I want to define my custom decorator that has user content in it. But it always fail when I try to set some control's name. I always get this exception when trying to do it:

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

I don't understand why that happens. Here's teh codez:

<UserControl x:Class="WpfApplication5.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"
             x:Name="control">
    <ContentPresenter Content="{Binding ElementName=control, Path=DataContext}" />
</UserControl>

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication5"
        Title="MainWindow" Height="350" Width="525">
    <local:UserControl1>
        <local:UserControl1.DataContext>
            <Button x:Name="butt" />
        </local:UserControl1.DataContext>
    </local:UserControl1>
</Window>

How to do that properly?

Poma
  • 8,174
  • 18
  • 82
  • 144

1 Answers1

0

You cannot name elements inside UserControls, someone considered this to be a bug but i do not know if this is the case, either way, this will not work. You could declare the Button as a resource of your Window and then insert it via StaticResource, then however the name will not be registered as a field in the window class.

Either way, do you really need the name?

Edit: Also see this question.

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400