WPF TryFindResource typeof Setter Trigger

FredGrit發表於2024-06-27
//xaml
<Window x:Class="WpfApp183.MainWindow"
        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:local="clr-namespace:WpfApp183" WindowState="Maximized"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <LinearGradientBrush x:Key="brush1">
            <GradientStop Offset="0" Color="Green"/>
            <GradientStop Offset="0.5" Color="Cyan"/>
            <GradientStop Offset="1" Color="Blue"/>
        </LinearGradientBrush>
        <Style TargetType="ListBox">
            <Setter Property="Width" Value="200"/>
            <Setter Property="SelectionMode" Value="Extended"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Width" Value="300"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Rectangle Height="100" Stroke="Black" Fill="{StaticResource brush1}">
            
        </Rectangle>
        <Ellipse StrokeThickness="20" Height="100" Stroke="{StaticResource brush1}">
            
        </Ellipse>
    </StackPanel>
</Window>



//xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 System.IO;


namespace WpfApp183
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var lbxStyle = this.TryFindResource(typeof(ListBox)) as Style;
            if (lbxStyle != null)
            {
                var setters = lbxStyle.Setters;
                if (setters != null && setters.Any())
                {
                    File.AppendAllText("Style.txt", "Setter value:\n");
                    foreach (var setter in setters)
                    {
                        var sv = setter as Setter;
                        if (sv!=null)
                        {
                            File.AppendAllText("Style.txt", $"{sv.Property},{sv.Value}\n"); 
                        } 
                    }
                }

                var triggers= lbxStyle.Triggers;
                if(triggers != null && triggers.Any())
                {
                    File.AppendAllText("Style.txt", "Trigger value:\n");
                    foreach (var trigger in triggers)
                    {
                        var tr = trigger as Trigger;
                        if(tr!=null)
                        {
                            File.AppendAllText("Style.txt", $"{tr.Property},{tr.Value}\n");
                        }
                    }
                }
            }
        }
    }
}

相關文章