I have below resource file:
namespace My.XAML
{
partial class MessageBoxResources: ResourceDictionary
{
public MessageBoxResources()
{
InitializeComponent(); // <--- Compiler throws error here
}
private void OnButtonMouseLeave(object sender, MouseEventArgs e)
{
var btn = (Button)sender;
var gradient = btn.Background as RadialGradientBrush;
if (gradient == null)
return;
gradient.GradientOrigin = new Point(0.5, 0.5);
gradient.Center = gradient.GradientOrigin;
}
private void OnButtonMouseMove(object sender, MouseEventArgs e)
{
var btn = (Button)sender;
var gradient = btn.Background as RadialGradientBrush;
if (gradient == null)
return;
Point pt = Mouse.GetPosition(btn);
RadialGradientBrush rgb_new = gradient.Clone();
rgb_new.GradientOrigin = new Point(pt.X / btn.ActualWidth, pt.Y / btn.ActualHeight);
rgb_new.Center = gradient.GradientOrigin;
btn.Background = (Brush)rgb_new;
}
}
}
Above resource file is attached to my Style file:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="My.XAML.MessageBoxResources">
<RadialGradientBrush x:Key="gradRadial" RadiusX="0.25">
<GradientStop Color="AliceBlue" Offset="0.0"/>
<GradientStop Color="LightSteelBlue" Offset="1.0"/>
</RadialGradientBrush>
<Style TargetType="Button"
x:Key="MessageBoxButtonStyle">
<Setter Property="Background" Value="{StaticResource gradRadial}"/>
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="Border" CornerRadius="0"
BorderBrush="#000" BorderThickness="1,1,1,1"
Background="{TemplateBinding Background}">
<ContentPresenter x:Name="contentPresenter"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalAlignment}" />
</Border>
<!-- TRIGGERS -->
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="BorderBrush" Value="AliceBlue" />
<Setter Property="Cursor" Value="Hand" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Border" Property="BorderBrush" Value="LightSteelBlue" />
<Setter Property="Background" Value="LightSteelBlue" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<EventSetter Event="MouseLeave" Handler="OnButtonMouseLeave"/>
<EventSetter Event="MouseMove" Handler="OnButtonMouseMove"/>
</Style>
</ResourceDictionary>
Compiler is throwing below message error within resource file, just in constructor:
The name 'InitializeComponent' does not exist in the current context
My current compilation action in resource file properties is set to "Compile". If I change it to "Resource" then it works, no compilation error appears but then another compilation error says that no method OnButtonMouseLeave is defined within My.XAML.MessageBoxResources. Also, this resource file is placed within a "Resources" folder. I wonder what is the correct compilation action that I should put for resource file, what is the correct compilation action?