LoginSignup
0
0

More than 1 year has passed since last update.

WPF Window or Control での初期表示とか

Last updated at Posted at 2023-01-20

コンストラクタでごにょごにょやるのは嫌だ

Views/MainWindow

<Window x:Class="WpfApp2.Views.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:WpfApp2"
        xmlns:prism="http://prismlibrary.com/"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        mc:Ignorable="d"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="MainWindow" Height="450" Width="800">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding LoadedCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Grid>
        <TextBlock Text="{Binding Message.Value}"></TextBlock>
    </Grid>
</Window>

ViewModels/MainWindowViewModel

using Prism.Mvvm;
using Reactive.Bindings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp2.ViewModels
{
    internal class MainWindowViewModel : BindableBase
    {
        public ReactiveCommand LoadedCommand { get; set; } = new ReactiveCommand();
        public ReactiveProperty<string> Message { get; set; } = new ReactiveProperty<string>();
        public MainWindowViewModel()
        {
            LoadedCommand.Subscribe(() => {
                Message.Value = $"Loaded {DateTime.Now:yyyy/MM/dd HH:mm:ss}";
            });
        }
    }
}

前に同じような記事を書いた気がするのはきっと気のせい。

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