4.CheckBoxとVisibility
戻るCheckBox
チェックをはずすとボタンとテキストボックスが消え、チェックを付けると表示される機能です。画面は下記の通りです。
チェックした時
クリックすると拡大します
チェックをはずした時
クリックすると拡大します
下記がボタン、チェックボックス、テキストボックスの定義です。
View
<CheckBox Content="IsVisible" Foreground="White" Width="70" Height="18" Margin="106,117,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" IsChecked="{Binding IsTextBoxVisible, UpdateSourceTrigger=PropertyChanged}"/> <Button Command="{Binding Path=SearchInquiryClearCommand}" Content="Button" Visibility="{Binding IsTextBoxVisible, Converter={StaticResource BoolToVisibility}}" IsEnabled="{Binding ButtCancelEnabled}" Margin="106,63,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="{x:Null}" Foreground="White" Width="177" Height="32"> <Button.Style> <Style TargetType="{x:Type Button}"> <Setter Property="Background" Value="DarkGreen"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border Background="{TemplateBinding Background}"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="DarkGoldenrod"/> </Trigger> </Style.Triggers> </Style> </Button.Style> </Button> <TextBox Width="70" Height="23" Margin="106,154,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Visibility="{Binding IsTextBoxVisible, Converter={StaticResource BoolToVisibility}}"/>
下記がフルコードです。
View
<ccl:CustomChromeWindow x:Class="LivetWPFChromeHelpDesk1.Views.Window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell" xmlns:ccl="clr-namespace:CustomChromeLibrary;assembly=CustomChromeLibrary" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" xmlns:l="http://schemas.livet-mvvm.net/2011/wpf" xmlns:core="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" xmlns:v="clr-namespace:LivetWPFChromeHelpDesk1.Views" xmlns:vm="clr-namespace:LivetWPFChromeHelpDesk1.ViewModels" WindowStartupLocation="CenterScreen" Title="Window2" Height="714" Width="1035"> <shell:WindowChrome.WindowChrome> <shell:WindowChrome ResizeBorderThickness="6" CaptionHeight="43" CornerRadius="0,0,0,0" GlassFrameThickness="0"> </shell:WindowChrome> </shell:WindowChrome.WindowChrome> <Window.Resources> <ResourceDictionary> <vm:CaptionButtonRectToMarginConverter x:Key="CaptionButtonMarginConverter"/> <BooleanToVisibilityConverter x:Key="BoolToVisibility"/> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Resources/Styles2.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> <Window.DataContext> <vm:ViewModel2/> </Window.DataContext> <i:Interaction.Triggers> <!--WindowのContentRenderedイベントのタイミングでViewModelのInitializeメソッドが呼ばれます--> <i:EventTrigger EventName="ContentRendered"> <l:LivetCallMethodAction MethodTarget="{Binding}" MethodName="Initialize"/> </i:EventTrigger> <!-- 下記がないと、タスクバーが1つにならない --> <i:EventTrigger EventName="Loaded"> <i:InvokeCommandAction Command="{Binding Path=Loaded}" CommandParameter="{Binding Mode=OneTime,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"/> </i:EventTrigger> <!-- <i:EventTrigger EventName="Closing"> <i:InvokeCommandAction Command="{Binding Path=Closing}" CommandParameter="{Binding Mode=OneTime,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"/> </i:EventTrigger> <l:InteractionMessageTrigger MessageKey="Close" Messenger="{Binding Messenger}"> <l:WindowInteractionMessageAction/> </l:InteractionMessageTrigger> --> </i:Interaction.Triggers> <!-- Grid-1 --> <Grid> <!-- WindowChrome Start --> <Border Grid.RowSpan="2" BorderThickness="3" BorderBrush="Black"> <Border.Background> <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="Black" Offset="1" /> </LinearGradientBrush> </Border.Background> </Border> <Border BorderThickness="3,3,3,1" BorderBrush="Black" Margin="{Binding Path=CaptionButtonMargin}"> <Border.Background> <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="Black" Offset="1" /> </LinearGradientBrush> </Border.Background> <!--Window Icon and Title--> <StackPanel Orientation="Horizontal" Margin="0" VerticalAlignment="Top"> <TextBlock Text=" Help Desk" FontFamily="Calibri" FontWeight="Bold" FontSize="26" Foreground="Blue" /> </StackPanel> </Border> <ccl:CaptionButtons /> <!-- WindowChrome End --> <!-- Grid-2 --> <!--Content--> <Grid Grid.Row="1"> <!-- ★★★ --> <TextBox Text="{Binding Path=txt本日}" TextAlignment="Center" Foreground="White" Background="Black" Height="17" HorizontalAlignment="Center" Name="txt本日" VerticalAlignment="Top" Width="81" Margin="809,3,123,0" /> <CheckBox Content="IsVisible" Foreground="White" Width="70" Height="18" Margin="106,117,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" IsChecked="{Binding IsTextBoxVisible, UpdateSourceTrigger=PropertyChanged}"/> <Button Command="{Binding Path=SearchInquiryClearCommand}" Content="Button" Visibility="{Binding IsTextBoxVisible, Converter={StaticResource BoolToVisibility}}" IsEnabled="{Binding ButtCancelEnabled}" Margin="106,63,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="{x:Null}" Foreground="White" Width="177" Height="32"> <Button.Style> <Style TargetType="{x:Type Button}"> <Setter Property="Background" Value="DarkGreen"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border Background="{TemplateBinding Background}"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="DarkGoldenrod"/> </Trigger> </Style.Triggers> </Style> </Button.Style> </Button> <TextBox Width="70" Height="23" Margin="106,154,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Visibility="{Binding IsTextBoxVisible, Converter={StaticResource BoolToVisibility}}"/> <!-- ★★★ --> </Grid> </Grid> </ccl:CustomChromeWindow>
ViewModelは下記の通りです。
ViewModel
using System; // ObservableCollection using System.Collections.Generic; using System.Linq; //INotifyPropertyChanged //PropertyChanged using System.ComponentModel; //参照設定が必要 //using System.Configuration; using Livet; using Livet.Commands; using Livet.Messaging; //CloseCommand using Livet.Messaging.Windows; //using Livet.Messaging.IO; //using Livet.EventListeners; //ICommand using System.Windows.Input; //MessageBox using System.Windows; // ListCollectionView using System.Windows.Data; using LivetWPFChromeHelpDesk1.Views; using LivetWPFChromeHelpDesk1.ViewModels; namespace LivetWPFChromeHelpDesk1.ViewModels { class ViewModel2 : ViewModel { #region 変更通知プロパティ //----------------------------------------------- public string txt本日 { get; set; } private bool isTextBoxVisible; public bool IsTextBoxVisible { get { return isTextBoxVisible; } set { isTextBoxVisible = value; RaisePropertyChanged("IsTextBoxVisible"); } } //----------------------------------------------- #endregion Window win = null; public ViewModel2() { Loaded = new Livet.Commands.ListenerCommand<Window>((w) => { if (NeedHideOwner && w.Owner != null && w.Owner.Visibility == Visibility.Visible) { win = w; //w.Owner.Hide(); } }); //Initialize()では表示されない txt本日 = Convert.ToString(DateTime.Today.ToShortDateString()); //---------------------- IsTextBoxVisible = true; //---------------------- } public bool NeedHideOwner { get; set; } public ICommand Loaded { get; private set; } public void Initialize() { if (win != null) win.Owner.Hide(); } #region CloseCommand private ViewModelCommand _CloseCommand; public ViewModelCommand CloseCommand { get { if (_CloseCommand == null) { _CloseCommand = new ViewModelCommand(Close); } return _CloseCommand; } } public void Close() { var window = Application.Current.Windows.OfType<Window>().SingleOrDefault((w) => w.IsActive); window.Close(); } #endregion } }