dotnet / dotnet/wpf

ControlTemplate NameScope bug when ToggleButton is inside a 'non-first' TabItem

Open
#8,936 0 comments 0 reactions 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

I have a custom style of `ToggleButton`, when I
- put the `ToggleButton` inside a `non-first` TabItem
- bind its `IsChecked` value to ViewModel
- sets the default value in the ViewModel to `True`

The app will crash with error message `System.InvalidOperationException: Name cannot be found in the namescope of System.Windows.Controls.ControlTemplate`

### Reproduction Steps

MainWindow.xaml:

```xaml


<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Viewbox>
<Border
x:Name="_Border"
Width="40"
Height="20"
Background="#FFFFFFFF"
CornerRadius="10">
<Border.Effect>
<DropShadowEffect
Direction="0"
Opacity="0.3"
ShadowDepth="0.5" />
</Border.Effect>
<Ellipse
x:Name="_Ellipse"
Margin="2,1,2,1"
HorizontalAlignment="Stretch"
Fill="#FFFFFFFF"
Stretch="Uniform"
Stroke="Gray"
StrokeThickness="0.2">
<Ellipse.Effect>
<DropShadowEffect
BlurRadius="10"
Direction="260"
Opacity="0.3"
ShadowDepth="1" />
</Ellipse.Effect>
</Ellipse>
</Border>
</Viewbox>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="Checked">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="_Border"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
To="#12a5fd"
Duration="0:0:0.1" />
<ThicknessAnimation
Storyboard.TargetName="_Ellipse"
Storyboard.TargetProperty="Margin"
To="20 1 2 1"
Duration="0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Unchecked">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="_Border"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
To="White"
Duration="0:0:0.1" />
<ThicknessAnimation
Storyboard.TargetName="_Ellipse"
Storyboard.TargetProperty="Margin"
To="2 1 2 1"
Duration="0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>


Test



```

MainWindow.xaml.cs

```cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}

public class ViewModel
{
public bool IsBusy { get; set; } = true;
}
```

### Expected behavior

`IsSelected` property of `ToggleButton` can bind to ViewModel.

### Actual behavior

The app crashes with error message 'System.InvalidOperationException: Name cannot be found in the namescope of System.Windows.Controls.ControlTemplate'

### Regression?

_No response_

### Known Workarounds

Use `Trigger` instead of `EventTrigger` in the `ControlTemplate`
```xaml


















```

### Impact

_No response_

### Configuration

- .NET 8.0
- Windows 10
- x64

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.