メインViewModelで何らかの子ウィンドウを起動しなければならず、この子ウィンドウのユーザーはテキストを入力するため、子ウィンドウを閉じた直後にメインのビューモデルでこのテキストを使用する必要があります。viewmodelから子ウィンドウをポップアップする方法
私はそれに結合した子ウィンドウのポップアップを持つことができていますが、子ウィンドウのビューモデルのIは、子ウィンドウを起動してい
メインビューモデルは次のとおりです。
private void OpenLayoutNameWindow()
{
LayOutName_VM chldWindow =new LayOutName_VM();
chldWindow.Show();
string layOutName = chldWindow.LayOutName;
MessageBox.Show("Name is:"+ layOutName); //this message box is popuped before i close the child window save button and its empty.
}
これは、の図です。子ウィンドウ:
<StackPanel>
<Label Margin="0,5,0,0" HorizontalAlignment="Center">Please name your new Layout</Label>
<TextBox Margin="0,5,0,0" Width="120" Height="20" HorizontalAlignment="Center" Text="{Binding LayOutName, Mode=TwoWay}"></TextBox>
<Button Margin="0,5,0,0" Width="80" Height="20" HorizontalAlignment="Center" Command="{Binding CloseWindowCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"> Save</Button>
</StackPanel>
これは、背後にあるコードです:
public partial class LayOutName : Window
{
public LayOutName()
{
InitializeComponent();
this.DataContext = new LayOutName_VM(); ;
}
}
これで子ウィンドウのビューモデル:クラスをViewModelにするために、子ウィンドウに入力したテキストを取得する方法
class LayOutName_VM : ViewModelBase
{
public ActionCommand<Window> CloseWindowCommand { get; private set; }
public LayOutName_VM()
{
this.CloseWindowCommand = new ActionCommand<Window>(this.SaveLayOutName);
}
private string layOutName;
public string LayOutName
{
get
{
return layOutName;
}
set
{
layOutName = value;
RaisePropertyChanged("LayOutName");
}
}
private void SaveLayOutName(Window wind)
{
wind.Close();
}
internal void Show()
{
LayOutName ly = new BrukerApp.LayOutName();
ly.Show();
}
}
。
MVVMを使用しようとしている場合は、コードの中に何も置いてはいけません。あなたは 'ViewModelBase'を持っていますが、MVVMライブラリの1つを使用していますか?もしそうなら、あなたは 'Mediator'パターンまたは' EventAggregator'パターンの実装を見ていきます。 –