首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用户控件的返回值对话框

用户控件的返回值对话框
EN

Stack Overflow用户
提问于 2017-06-13 02:31:44
回答 0查看 893关注 0票数 0

我正在尝试实现一个对话框来要求用户输入。我创建了一个用户控件并将数据上下文绑定到一个ViewModel。我要求用户从列表中选择一项。我想把这个选择传回主程序。

代码语言:javascript
运行
复制
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)

代码语言:javascript
运行
复制
<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)

代码语言:javascript
运行
复制
    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
}
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44506402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档