6.引数のあるボタン
戻る引数のあるボタン
テキストボックスに入力した文字をメッセージボックスに表示する機能です。画面は下記の通りです。
起動時
クリックすると拡大します
下記がテキストボックスとボタンの定義です。Command(MethodName)はShowMessageBoxです。
View
<TextBox Text="{Binding Path=txtMessage, Mode=TwoWay}" VerticalContentAlignment="Center" Foreground="White" Background="Black" Height="24" HorizontalAlignment="Left" Margin="66,104,0,0" VerticalAlignment="Top" Width="270" /> <Button Content="引数のあるボタン" Margin="66,178,0,0" Foreground="White" FontSize="16" Height="35" Style="{StaticResource CommandTemplate}" VerticalAlignment="Top" HorizontalAlignment="Left" Width="270"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <l:LivetCallMethodAction MethodName="ShowMessageBox" MethodTarget="{Binding}" MethodParameter="{Binding txtMessage}" /> </i:EventTrigger> </i:Interaction.Triggers> </Button>
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 ViewModel11 : ViewModel { #region 変更通知プロパティ //----------------------------------------------- //public string txtMessage { get; set; } private string _txtMessage; public string txtMessage { get { return _txtMessage; } set { _txtMessage = value; RaisePropertyChanged("txtMessage"); } } // private string _txtSearchInquiry; public string txtSearchInquiry { get { return _txtSearchInquiry; } set { if (_txtSearchInquiry != value) { _txtSearchInquiry = value; RaisePropertyChanged("txtSearchInquiry"); } } } // public string txt本日 { get; set; } public bool IsEnabled { get; set; } // public string txtTitle2 { get; set; } private string _txtTitle2; public string txtTitle2 { get { return _txtTitle2; } set { _txtTitle2 = value; RaisePropertyChanged("txtTitle2"); } } //----------------------------------------------- #endregion Window win = null; public ViewModel11() { 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()); } public bool NeedHideOwner { get; set; } public ICommand Loaded { get; private set; } public ICommand Closing { get; private set; } public void Initialize() { if (win != null) win.Owner.Hide(); } public void ShowMessageBox(string value) { // ViewのMethodParameterを受け取る MessageBox.Show(value); } #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 } }