佑佑ソフト

表示/表示切替

MainWindow.xaml
<Window x:Class="WpfDataTrigger1.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:WpfDataTrigger1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <Button Name="btn1" Height="25" Click="Visible_Click">Visibility="Visible"</Button>
        <Button Name="btn2" Height="25" Click="Hide_Click">Visibility="Hidden"</Button>
        <Grid Name="tb1" Visibility="{Binding BindView}">
            <Border>
                <StackPanel Orientation="Horizontal">
                    <TextBlock> label</TextBlock>
                    <TextBlock Margin="10,0,0,0">Hello World!</TextBlock>
                </StackPanel>
            </Border>
        </Grid>

        <!--表示/非表示切替--> 
        <Grid  Visibility="{Binding BindView}">
            <Border>
                <StackPanel Orientation="Horizontal">
                    <TextBlock> label</TextBlock>
                    <TextBlock Margin="10,0,0,0" Text="{Binding BindString}">
                    </TextBlock>
                </StackPanel>
            </Border>
        </Grid>

        <StackPanel Width="60">
            <StackPanel.Resources>
                <Style TargetType="TextBox">
                    <Setter Property="Background" Value="LightGray" />

                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="LightBlue" />
                        </Trigger>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="Background" Value="LightPink" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Resources>
            <TextBox Text="テキスト" Height="32" Margin="-36,0,-49,0" RenderTransformOrigin="0.5,0.5" >
                <TextBox.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform/>
                        <SkewTransform/>
                        <RotateTransform Angle="-0.653"/>
                        <TranslateTransform/>
                    </TransformGroup>
                </TextBox.RenderTransform>
            </TextBox>
        </StackPanel>
    </StackPanel>
</Window>
  
MainWindow.xaml.cs
using System.Windows;
using System.Windows.Controls;

namespace WpfDataTrigger1
{
    /// 
    /// MainWindow.xaml の相互作用ロジック
    /// 
    public partial class MainWindow : Window
    {
        MainWindowViewModel ViewModel = new MainWindowViewModel();
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this.ViewModel;
        }
        //表示
        private void Visible_Click(object sender, RoutedEventArgs e)
        {
            this.ViewModel.VisibleCommand();

        }
        //非表示
        private void Hide_Click(object sender, RoutedEventArgs e)
        {
            this.ViewModel.HideCommand();

        }
    }
}
  
MainWindowViewModel.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfDataTrigger1
{
    abstract class MainWindowViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string value)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value));
        }

    }

    class MainWindowViewModel: MainWindowViewModelBase
    {
        public string BindString { get; set; } = "表示";
        public Visibility BindView { get; set; } = Visibility.Hidden;

        public void VisibleCommand()
        {
            BindString = "表示";
            BindView= Visibility.Visible;
            OnPropertyChanged(nameof(BindString));
            OnPropertyChanged(nameof(BindView));
        }
        public void HideCommand()
        {
            BindString = String.Empty;
            BindView = Visibility.Hidden;
            OnPropertyChanged(nameof(BindString));
            OnPropertyChanged(nameof(BindView));
        }
    }
}