I am new to WPF and I am making a WPF project for university with custom made buttons.
<Style x:Key="Button113" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Path x:Name="path" Fill="#FF8E2424" Stretch="Fill" Opacity="0.0" Stroke="{x:Null}" Data="M575.33333,304.87333 C651.33333,305.20667 651.33365,305.20699 651.33365,305.20699 620.33383,380.13463 620.16716,380.46764 620.16716,380.46764 576.16698,380.30114 576.16698,380.30114 576.16698,380.30114 575.50031,305.04016 575.33333,304.87333 575.33333,304.87333 z"/>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" TargetName="path" Value="DarkSlateBlue"/>
<Setter Property="Opacity" TargetName="path" Value="0.4"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and then I create the button using the style
<Button x:Name="button113" HorizontalAlignment="Right" Margin="0,0,16.25,27.416" Style="{DynamicResource Button113}" VerticalAlignment="Bottom" Width="78" Height="77.834" Content="" ToolTip="" ToolTipOpening="Button_ToolTipOpening" Click="form6_roomBtn_Click"/>
I have had some trouble making the button change color on mouse hover, but somehow I managed to do it with trigger.
Now I would like to change the background property of the button from my c# code but nothing happens. How do I access the fill property of the path from my code? (i tried using path.Fill but path is not recognized).
Thank you
Thank you
Accessing items within a ControlTemplate becomes a headache, especially if the class you are templating is not defined by you. I would suggest trying this instead:
<Style x:Key="Button113" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Path x:Name="path" Fill="{TemplateBinding Background}" Stretch="Fill" Opacity="0.0" Stroke="{x:Null}" Data="M575.33333,304.87333 C651.33333,305.20667 651.33365,305.20699 651.33365,305.20699 620.33383,380.13463 620.16716,380.46764 620.16716,380.46764 576.16698,380.30114 576.16698,380.30114 576.16698,380.30114 575.50031,305.04016 575.33333,304.87333 575.33333,304.87333 z"/>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" TargetName="path" Value="DarkSlateBlue"/>
<Setter Property="Opacity" TargetName="path" Value="0.4"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This binds the fill of the path to the Background property of the Button. Now you can just change the Background property at will, without having to get into the nastiness of getting stuff from inside your template.