LoginSignup
0
0

More than 5 years have passed since last update.

Visual Studio / WPF > Resources > 実装 > ボタンを押したら色を変更する > FindResource()使用 | TryFindResource()

Last updated at Posted at 2017-06-14
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community

link

XAML Resources
Resources and Code

実装

Resourceを定義して、ボタンを押したときにStyleを割り当てる実装をしてみた。

MainWindow.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;

namespace _170614_t1939_resourceStyle
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void GoodButton_Click(object sender, RoutedEventArgs e)
        {
            uxTitle.Style = this.FindResource("TitleText") as Style;
        }
    }
}
MainWindow.xaml
<Window x:Class="_170614_t1939_resourceStyle.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:_170614_t1939_resourceStyle"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="TextBlock" x:Key="TitleText">
            <Setter Property="Background" Value="LightGreen"/>
            <Setter Property="FontSize" Value="18"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="FontFamily" Value="Trebuchet MS"/>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="10*"/>
            <RowDefinition Height="28*"/>
            <RowDefinition Height="8*"/>
            <RowDefinition Height="89*"/>
        </Grid.RowDefinitions>
        <TextBlock TextWrapping="Wrap" 
                   Text="Kiita"/>
        <TextBlock Grid.Row="1" TextWrapping="Wrap" Height="Auto"
                   x:Name="uxTitle"
                   Text="Visual Studio / WPF > link > XAML Resources"/>
        <Button Content="Good!" 
                x:Name="GoodButton"
                Click="GoodButton_Click"
                HorizontalAlignment="Left" Margin="419,32,0,0" Grid.Row="1" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window>

実行例

ボタン押下前
2017-06-14_20h14_56.png

ボタン押下後
2017-06-14_20h16_48.png

link

以下のリンクは今後参考になるかもしれません。

ViewModelでStyleを指定する方法。StyleConverterでFindResource汚染を回避

TryFindResource

Resources and Code

FindResource raises an exception; TryFindResource will not raise an exception but returns null.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0