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

```txt:動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
```

### link

[XAML Resources](https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/xaml-resources)
[Resources and Code](https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/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](https://qiita-image-store.s3.amazonaws.com/0/32870/94e9a169-1d2e-4a38-eb62-a11ee67dfadd.png)

ボタン押下後
![2017-06-14_20h16_48.png](https://qiita-image-store.s3.amazonaws.com/0/32870/cc4ee2f5-dc92-f0db-57bf-4b067f6f1b00.png)

### link

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

[ViewModelでStyleを指定する方法。StyleConverterでFindResource汚染を回避](http://qiita.com/takanemu/items/78da9744291ce0c8a839)

### TryFindResource

[Resources and Code](https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/resources-and-code)

> [FindResource](https://docs.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.findresource?view=netframework-4.7) raises an exception; [TryFindResource](https://docs.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.tryfindresource?view=netframework-4.7) will not raise an exception but returns null.