如何从代码隐藏中修改资源的属性
本文关键字:修改 资源 属性 隐藏 代码 | 更新日期: 2023-09-27 18:33:49
我创建了一个用户控件元素,该元素具有一个具有单独样式的按钮。
在这种样式中,我有一个名为"矩形"的矩形。
现在我想从隐藏的代码中更改此矩形的可见性。
我该怎么做?
或者一般而言:如何从代码隐藏中更改资源的任何属性?
Button_Field.xaml
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
mc:Ignorable="d"
x:Class="WpfControlLibrary_Battleship.Button_Field"
x:Name="UserControl"
d:DesignWidth="512" d:DesignHeight="512" Width="Auto" Height="Auto">
<UserControl.Resources>
<Style x:Key="ButtonStyle_FieldWater" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid x:Name="Grid">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.Background>
<ImageBrush ImageSource="Images/Water.jpg"/>
</Grid.Background>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition From="MouseOver" GeneratedDuration="0">
<Storyboard>
<DoubleAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Path_1"/>
<DoubleAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Path_2"/>
<DoubleAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Path_3"/>
<DoubleAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Path_4"/>
</Storyboard>
</VisualTransition>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Path_1">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Path_2">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Path_3">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Path_4">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed"/>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Path x:Name="Path_1" Data="M2.5,2.5 L82.833332,2.5 M2.5000006,2.5 L2.5000006,82.833329" HorizontalAlignment="Stretch" Height="Auto" Margin="0" StrokeStartLineCap="Square" Stretch="Fill" StrokeEndLineCap="Square" Stroke="#FF9B0000" StrokeThickness="5" VerticalAlignment="Stretch" Width="Auto" Grid.ColumnSpan="2" Grid.RowSpan="2" Opacity="0"/>
<Path x:Name="Path_2" Grid.Column="6" Data="M2.5,2.5 L82.833356,2.5 M82.833333,2.5 L82.833333,82.833335" HorizontalAlignment="Stretch" Height="Auto" Margin="0" StrokeStartLineCap="Square" Stretch="Fill" StrokeEndLineCap="Square" Stroke="#FF9B0000" StrokeThickness="5" VerticalAlignment="Stretch" Width="Auto" Grid.ColumnSpan="2" Grid.RowSpan="2" Opacity="0"/>
<Path x:Name="Path_3" Data="M2.5,2.5 L2.5,82.833328 M2.5,82.833323 L82.833336,82.833323" HorizontalAlignment="Stretch" Margin="0" Grid.Row="6" StrokeStartLineCap="Square" Stretch="Fill" StrokeEndLineCap="Square" Stroke="#FF9B0000" StrokeThickness="5" Width="Auto" Grid.ColumnSpan="2" Grid.RowSpan="2" Opacity="0"/>
<Path x:Name="Path_4" Grid.Column="6" Data="M2.5000001,168.16667 L168.16665,168.16667 M168.16666,2.5000051 L168.16666,168.1667" HorizontalAlignment="Stretch" Height="Auto" Margin="0" Grid.Row="6" StrokeStartLineCap="Square" Stretch="Fill" StrokeEndLineCap="Square" Stroke="#FF9B0000" StrokeThickness="5" VerticalAlignment="Stretch" Width="Auto" Grid.ColumnSpan="2" Grid.RowSpan="2" Opacity="0"/>
<Rectangle x:Name="Rectangle" Grid.ColumnSpan="8" Fill="Red" Margin="0" Grid.RowSpan="8" Stroke="Black" Opacity="0.495">
<Rectangle.Visibility>
<Visibility>Hidden</Visibility>
</Rectangle.Visibility>
</Rectangle>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True"/>
<Trigger Property="IsDefaulted" Value="True"/>
<Trigger Property="IsMouseOver" Value="True"/>
<Trigger Property="IsPressed" Value="True"/>
<Trigger Property="IsEnabled" Value="False"/>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Button x:Name="Button" Content="Button" Style="{DynamicResource ButtonStyle_FieldWater}"/>
</UserControl>
Button_Field.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Battleship_LOGIC;
namespace WpfControlLibrary_Battleship
{
/// <summary>
/// Interaktionslogik für Button_Field.xaml
/// </summary>
public partial class Button_Field : UserControl
{
private Battleship_LOGIC.Game_Logic.Point _point;
public Button_Field(Battleship_LOGIC.Game_Logic.Point point)
{
InitializeComponent();
_point = point;
}
public void redBackground()
{
//Set Visibility of Rectangle to "Visible"
}
public Battleship_LOGIC.Game_Logic.Point Point
{
get { return _point; }
}
}
}
试试这个:
var rect = (Rectangle)Button.Template.FindName("Rectangle", Button);
rect.Visibility = System.Windows.Visibility.Visible;