Why does adding ContentControl cause my application to go into interrupt mode?

Brief

I created a beautiful WindowChrome style to apply to my windows. However, when I add a ContentControl to my style, the application goes into interrupt mode.

I compiled the code from this youtube video , this article , this SO question and the Microsoft documentation and I’ll get the following code.

Note The code below is considered relevant , because the application cannot work with any of these parts (yes, I know that it can work without code, but it annoys the need to stop the application from Visual Studio, and not the close button - which I am also trying to execute) . I actually reduced the code below to make it easier to work with.


code

Window.xaml

 <Style x:Key="TestWindow" TargetType="{x:Type Window}"> <Setter Property="Background" Value="#FF222222"/> <Setter Property="BorderBrush" Value="WhiteSmoke"/> <Setter Property="BorderThickness" Value="5,30,5,5"/> <Setter Property="WindowChrome.WindowChrome"> <Setter.Value> <WindowChrome CaptionHeight="20" CornerRadius="0" GlassFrameThickness="0,0,0,-1" NonClientFrameEdges="None" ResizeBorderThickness="5" UseAeroCaptionButtons="True"/> </Setter.Value> </Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Window}"> <Grid> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <AdornerDecorator> <ContentPresenter/> </AdornerDecorator> </Border> <DockPanel LastChildFill="True" VerticalAlignment="Top" Height="30"> <StackPanel DockPanel.Dock="Right" Orientation="Horizontal" VerticalAlignment="Center"> <Button x:Name="Button_Close" WindowChrome.IsHitTestVisibleInChrome="True" Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}" Click="CloseClick"> <ContentControl Template="{StaticResource Icon_Close}" Height="10"/> </Button> </StackPanel> <StackPanel DockPanel.Dock="Left" Orientation="Horizontal" VerticalAlignment="Center"> <Image x:Name="PART_WindowCaptionIcon" Width="16" Height="16" Margin="0,0,6,0" Source="{TemplateBinding Icon}"/> <TextBlock x:Name="PART_WindowCaptionText" Margin="6,0,0,0" Padding="0"> <Run BaselineAlignment="Center" Text="{TemplateBinding Title}" Foreground="Black"/> </TextBlock> </StackPanel> </DockPanel> </Grid> <ControlTemplate.Triggers> <Trigger SourceName="PART_WindowCaptionIcon" Property="Source" Value="{x:Null}"> <Setter TargetName="PART_WindowCaptionIcon" Property="Visibility" Value="Collapsed"/> <Setter TargetName="PART_WindowCaptionText" Property="Margin" Value="5,0,0,0"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> 

Icons.xaml

Window.xaml refers to this file for ContentControl Template attribute values ​​through App.xaml .

 <ControlTemplate x:Key="Icon_Close"> <Viewbox> <Polygon Points="357,35.7 321.3,0 178.5,142.8 35.7,0 0,35.7 142.8,178.5 0,321.3 35.7,357 178.5,214.2 321.3,357 357,321.3 214.2,178.5" Fill="Black"/> </Viewbox> </ControlTemplate> 

Window.xaml.cs

 public partial class Window : ResourceDictionary { public Window() { InitializeComponent(); } private void CloseClick(object sender, RoutedEventArgs e) { var window = (System.Windows.Window)((FrameworkElement)sender).TemplatedParent; window.Close(); } } 

Problem

When the line <ContentControl Template="{StaticResource Icon_Close}" Height="10"/> (line 38) is present, the following message is received. When the same line is deleted / commented out, the application starts without going into interrupt mode.

Message: application is in break mode

In the output window, I get the following messages:

 An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception. 

Questions

This code worked when placed directly in the XAML code for Window , but the moment I try to put it in the template, it fails.

My questions:

  • Why does my application go into interrupt mode when the ContentControl is placed in the Window template?
  • How can I solve this problem?
    • Please note that I have to use my ControlTemplate from the Icons.xaml file and that the call to this content should remain in the Style window (and not in the actual xaml window).
+5
source share
1 answer

Brief

The problem arose due to the incorrect order of my styles in the answer to this question . I mark my question as a duplicate of this, but felt that I should share this as an answer if it helps someone else.

I like that Microsoft cannot handle this with the corresponding exception, and that you need to hit your head against a wall until your head or wall breaks.

code

My App.xaml contains the following code in ResourceDictionary.MergedDictionaries

 <ResourceDictionary Source="pack://application:,,,/MyProject;component/Window.xaml"/> <ResourceDictionary Source="pack://application:,,,/MyProject;component/Icons.xaml"/> 

I changed the order to the next

 <ResourceDictionary Source="pack://application:,,,/MyProject;component/Icons.xaml"/> <ResourceDictionary Source="pack://application:,,,/MyProject;component/Window.xaml"/> 
+1
source

Source: https://habr.com/ru/post/1274081/


All Articles