Hallo zusammen,
ich habe ein "eigenes" Control, dass von GroupBox ableitet.
<GroupBox x:Class="Test.OwnGroupBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<GroupBox.Style>
<Style TargetType="{x:Type GroupBox}">
<Setter Property="Margin" Value="2"/>
</Style>
</GroupBox.Style>
</GroupBox>
Wenn ich nun diese GroupBox wie folgt benutze
<UserControl x:Class="Test.BasicInformationUserControl"
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"
xmlns:controls="clr-namespace:Test"
mc:Ignorable="d" x:Name="_basicInformationUserControl"
x:Uid="BasicInformationUserControl">
<controls:OwnGroupBox Header="General" x:Uid="GeneralGroupBox">
<TextBlock Text="Status" x:Name="xxx"/>
<ContentPresenter Content="{Binding Height, ElementName=_basicInformationUserControl}"/>
</controls:OwnGroupBox>
</UserControl>
bekomme ich diesen Fehler> Fehlermeldung:
Cannot set Name attribute value 'xxx' on element 'TextBlock'. 'TextBlock' is under the scope of element 'OwnGroupBox', which already had a name registered when it was defined in another scope.
Und das Binding im ContentPresenter geht auch nicht. Ersetze ich nun controls:OwnGroupBox durch GroupBox, bekomme ich keinen Fehler mehr und das Binding klappt.
Leider verstehe ich nicht wieso eine Ableitung dies zur Folge hat. Hätte jemand eine Idee, warum dies so ist.
Viele Grüße
Quanue
Das geht leider nicht so. Lies mal How to create a WPF UserControl with NAMED content sowie den dadrin erwähnten Blog WPF: Cannot set Name attribute.
Auch bei Probleme beim WPF Databinding ist das Problem angesprochen.
Hallo Th69,
vielen Dank für deine Links. Jetzt ist mir einiges klarer.
Schöne Grüße
Quaneu
...Ersetze ich nun controls👍wnGroupBox durch GroupBox, bekomme ich keinen Fehler mehr...
Kleine Ergänzung:
<GroupBox>
<TextBlock ... />
<ContentPresenter ... />
</GroupBox>
wird auch einen Fehler liefern.
Zwar nicht den von dir erwähnten, aber diesen:> Fehlermeldung:
The object 'GroupBox' already has a child and cannot add 'ContentPresenter'. 'GroupBox' can accept only one child.
Man kann direkt in der GroupBox nur ein Element definieren. Möchte man mehr, muss man erst einen StackPanel, Grid o.ä. definieren. =)