2017-06-12 117 views
0

我想實現一個對話框來詢問用戶輸入。我創建了一個用戶控件並將數據上下文綁定到ViewModel。我要求用戶從列表中選擇一個項目。我想將選擇傳遞迴主程序。來自用戶控制對話框的返回值

try 
{ 
    var ins = from Instrument in InstrumentList where Instrument.Comport == result.Comport select Instrument; 
    result.OperatorName = ins.FirstOrDefault().OperatorName; 
    result.OperatorID = ins.FirstOrDefault().OperatorID; 
} 
catch (Exception) 
{ 
    //Open a control to prompt for instrument. 
    Window window = new Window 
    { 
     Title = "Associate Com Port", 
     Content = new MyDialogView(), 
     SizeToContent = SizeToContent.WidthAndHeight, 
     ResizeMode = ResizeMode.NoResize 
    }; 
    window.ShowDialog(); 
    //var inst= ValueFromDialogBox 
    //WriteInstrumentToConfigFile(inst); 
    //Set proper Instrument/Comport pair 
    GetAdditionalData(result); 
} 

所以我需要的是ValueFromDialogBox。我不介意能夠獲得多個價值。如果我可以傳回一個對象,那麼我可以做任何我想做的事情。

這裏是XAMLfor對話框(MyDialogView)

<UserControl.DataContext> 
    <vm:MyDialogViewModel/> 
</UserControl.DataContext>  

<UserControl.Resources> 
    <ResourceDictionary Source="./MainWindowResources.xaml" /> 
</UserControl.Resources> 

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="3*"/> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition /> 
    </Grid.RowDefinitions> 

    <TextBlock Grid.Column="0" 
       Grid.ColumnSpan="2" 
       Grid.Row="0" 
       Style="{StaticResource textStyle}" HorizontalAlignment="Center" 
       Background="AliceBlue"> 
     <Run Text="What instrument are you using?" /> 
    </TextBlock> 

    <TextBlock Grid.Column="0" 
       Grid.Row="1" 
       Style="{StaticResource textStyle}" 
       VerticalAlignment="Center" 
       Background="AliceBlue"> 
     <Run Text="Select the Instrument number from the list."/> 
    </TextBlock> 

    <ListBox Grid.Column="1" 
      Grid.Row="1" 
      SelectedItem="{Binding InstrumentSelected}" 
      ItemsSource="{Binding ListOfInstruments}"/> 
    </Grid> 

這裏是視圖模型(MyDialogViewModel)

public class MyDialogViewModel : ViewModelBase 
{ 
    #region Fields 
    string prompt; 
    string comResponse; 
    string comport; 
    private string selectedInstrument; 
    private ObservableCollection<string> listOfInstruments; 

    #endregion 

    #region Properties 

    public string Prompt 
    { 
     get { return prompt; } 
     set 
     { 
      prompt = value; 
      RaisePropertyChanged(); 

     } 
    } 

    public string ComResponse 
    { 
     get { return comResponse; } 
     set 
     { 
      comResponse = value; 
      RaisePropertyChanged(); 

     } 
    } 

    public string ComPort 
    { 
     get { return comport; } 
     set 
     { 
      comport = value; 
      RaisePropertyChanged(); 
     } 
    } 

    public string InstrumentSelected 
    { 
     get { return selectedInstrument; } 

     set 
     { 
      if (value == selectedInstrument) 
       return; 
      selectedInstrument = value; 

      base.RaisePropertyChanged(); 
     } 
    } 

    public ObservableCollection<string> ListOfInstruments 
    { 
     get { return listOfInstruments; } 
     set 
     { 
      listOfInstruments = value; 
      RaisePropertyChanged(); 

     } 
    } 
    #endregion 

    #region Commands 


    #endregion 

    #region Methods 



    #endregion 

    #region Constructors 

    public MyDialogViewModel() 
    { 
     listOfInstruments = new ObservableCollection<string>{ 
      "Instrument 1", "Instrument 2", "Instrument 3", "Instrument 4" 
     }; 
    } 

    public MyDialogViewModel(ref MainWindowViewModel mwvm) 
    { 
     listOfInstruments = new ObservableCollection<string>{ 
      "Instrument 1", "Instrument 2", "Instrument 3", "Instrument 4" 
     }; 
    } 
    #endregion 
} 
+0

如果您希望我們幫助您獲得「ValueFromDialogBox」,請盡力告訴我們「ValueFromDialogBox」是什麼。它是窗口內容的屬性嗎?如果您希望我們幫助您查找窗口內容的屬性,則必須告訴我們有關窗口內容及其屬性的信息。 –

+0

這是一個屬性。它是一個列表框中的字符串。 ListBox SelectedItem綁定到ViewModel屬性SelectedInstrument。當ViewModel被實例化時,ListBox被填充。 – ScottinTexas

回答

1
//Open a control to prompt for instrument. 
Window window = new Window 
{ 
    Title = "Associate Com Port", 
    Content = new MyDialogView(), 
    SizeToContent = SizeToContent.WidthAndHeight, 
    ResizeMode = ResizeMode.NoResize 
}; 
window.ShowDialog(); 

// The view is the window content. 
var view = (MyDialogView)window.Content; 

// The view XAML created an instance of MyDialogViewModel and assigned it to 
// the view's DataContext. 
MyDialogViewModel dlgVM = (MyDialogViewModel)view.DataContext; 

// Now you've got the viewmodel that was used in the dialog, with all its 
// properties intact. 
MessageBox.Show($"Instrument selected was {dlgVM.InstrumentSelected}"); 

如何關閉該窗口在這種情況下, :

用戶控件XAML:

<StackPanel 
    Orientation="Horizontal" 
    > 
    <Button 
     Content="_OK" 
     Click="OKButton_Click" 
     /> 
    <Button 
     Content="_Cancel" 
     IsCancel="True" 
     /> 
</StackPanel> 

用戶控件後臺代碼:

private void OKButton_Click(object sender, RoutedEventArgs e) 
{ 
    Window.GetWindow(sender as DependencyObject).DialogResult = true; 
} 

取消按鈕的作品,如果你只是設置IsCancel="True"就可以了。在這種情況下,ShowDialog()將返回false。在明確設置DialogResult的情況下,ShowDialog()將返回您分配給DialogResult的布爾值,除非它是nullDialogResult = null不會關閉窗口。

+0

工作就像一個魅力。謝謝。現在關閉對話框。我用窗戶關閉X來穿過它。我現在需要做的是實現一個OK按鈕來關閉對話框。但是,我得到了重要的部分感謝你。 – ScottinTexas

+0

@ScottinTexas見更新。 –