「東雲 忠太郎」の平凡な日常のできごと

「東雲 忠太郎」の平凡な日常のできごと

2024.06.09
XML
カテゴリ: WPFC#.NET


C# WPFでDynamicResourceを使用することで、リソースの値が動的に変更されたときにUIが自動的に更新されるようにすることができます。以下に、DynamicResourceを使用したよく利用されるサンプルを示します。


### 1. テーマの動的な変更




**App.xaml**

```xml

<Application x:Class="WpfApp.App"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             StartupUri="MainWindow.xaml">

    <Application.Resources>

        <SolidColorBrush x:Key="ButtonBackground" Color="LightBlue"/>

    </Application.Resources>

</Application>

```


**MainWindow.xaml**

```xml

<Window x:Class="WpfApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="450" Width="800">

    <Grid>

        <StackPanel>

            <Button Content="Dynamic Button" Background="{DynamicResource ButtonBackground}" Width="200" Height="50" Margin="10"/>

            <Button Content="Change Color" Click="ChangeColor_Click" Width="200" Height="50" Margin="10"/>

        </StackPanel>

    </Grid>

</Window>

```


**MainWindow.xaml.cs**

```csharp

using System.Windows;

using System.Windows.Media;


namespace WpfApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }


        private void ChangeColor_Click(object sender, RoutedEventArgs e)

        {

            Application.Current.Resources["ButtonBackground"] = new SolidColorBrush(Colors.LightCoral);

        }

    }

}

```


この例では、「Dynamic Button」の背景色が最初はLightBlueに設定されていますが、「Change Color」ボタンをクリックするとLightCoralに変更されます。


### 2. 動的なスタイル変更


スタイルを動的に変更する例です。この例では、ボタンのスタイルを動的に変更します。


**App.xaml**

```xml

<Application x:Class="WpfApp.App"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             StartupUri="MainWindow.xaml">

    <Application.Resources>

        <Style x:Key="DynamicButtonStyle" TargetType="Button">

            <Setter Property="Background" Value="LightBlue"/>

            <Setter Property="Foreground" Value="White"/>

            <Setter Property="FontSize" Value="16"/>

            <Setter Property="Margin" Value="5"/>

        </Style>

    </Application.Resources>

</Application>

```


**MainWindow.xaml**

```xml

<Window x:Class="WpfApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="450" Width="800">

    <Grid>

        <StackPanel>

            <Button Content="Dynamic Style Button" Style="{DynamicResource DynamicButtonStyle}" Width="200" Height="50"/>

            <Button Content="Change Style" Click="ChangeStyle_Click" Width="200" Height="50"/>

        </StackPanel>

    </Grid>

</Window>

```


**MainWindow.xaml.cs**

```csharp

using System.Windows;

using System.Windows.Media;


namespace WpfApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }


        private void ChangeStyle_Click(object sender, RoutedEventArgs e)

        {

            var newStyle = new Style(typeof(Button));

            newStyle.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.LightCoral));

            newStyle.Setters.Add(new Setter(Button.ForegroundProperty, Brushes.Black));

            newStyle.Setters.Add(new Setter(Button.FontSizeProperty, 18.0));

            newStyle.Setters.Add(new Setter(Button.MarginProperty, new Thickness(5)));


            Application.Current.Resources["DynamicButtonStyle"] = newStyle;

        }

    }

}

```


この例では、「Dynamic Style Button」のスタイルが最初はLightBlueの背景色に設定されていますが、「Change Style」ボタンをクリックするとLightCoralの背景色に変更されます。


DynamicResourceを使用すると、アプリケーションのリソースが動的に変更されたときにUIが自動的に更新されるため、ユーザーインターフェースの動的な変更が必要なシナリオで非常に便利です。






お気に入りの記事を「いいね!」で応援しよう

Last updated  2024.06.09 11:56:51


【毎日開催】
15記事にいいね!で1ポイント
10秒滞在
いいね! -- / --
おめでとうございます!
ミッションを達成しました。
※「ポイントを獲得する」ボタンを押すと広告が表示されます。
x
X

© Rakuten Group, Inc.
X
Mobilize your Site
スマートフォン版を閲覧 | PC版を閲覧
Share by: