wpf 如何从DialogService.ShowDialog()中获取int值并将该值赋给调用ShowDialog函数的视图?

mkshixfv  于 2023-03-13  发布在  其他
关注(0)|答案(1)|浏览(168)

我的dialogView由listbox和2个按钮组成,listbox的itemsource绑定到Linq-to-SQL数据库表的ObservableCollection类,接受按钮将接受从listbox中选择的项目,关闭按钮将关闭对话框窗口。我已经将int?变量绑定到listbox的SelectedIndex,这将有助于从ObservableCollection类中检索值。
DialogViewModel.cs

private static AssignDatabaseDataContext _adpDC = new AssignDatabaseDataContext();
private readonly ObservableRateList rateList;

public int SelectedRateItemIndex { get; set; }
public int? RateSelected { get; set; }

private int? selectedratevalue;
public int? SelectedRateValue 
{
    get 
    {
        selectedratevalue = rateList[3].RateValue;
        return selectedratevalue; 
    }
    set => this.SetProperty(ref this.selectedratevalue, value);
}

public int RateValueSelect
{
    get { return SelectedRateValue == null ? Convert.ToInt32((string)null) : Convert.ToInt32(rateList[SelectedRateItemIndex].RateValue); }
}

public string Title => "";

public ObservableCollection<RateTable> rateTables { get; set; }

public DelegateCommand CloseCommand { get; set; }
public DelegateCommand AcceptCommand { get; set; }

public event Action<IDialogResult> RequestClose;

public RateSelectViewModel()
{
    this.rateList = new ObservableRateList(_adpDC);
    this.rateTables = rateList;//new ObservableCollection<RateTable>();

    this.CloseCommand = new DelegateCommand(this.CloseDialog);
    this.AcceptCommand = new DelegateCommand(this.AcceptRateResult);
}

public bool CanCloseDialog()
{
    return true;
}

private void CloseDialog()
{
    this.RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
}

public void OnDialogClosed()
{
    //throw new NotImplementedException();
    this.RaisePropertyChanged(nameof(RateSelected));
}

public void OnDialogOpened(IDialogParameters parameters)
{
    this.RateSelected = parameters.GetValue<int>(DialogNames.RateSelected);
    this.rateTables = parameters.GetValue<ObservableRateList>(DialogNames.RateDialogList);
    this.RaisePropertyChanged(nameof(RateSelected));

    //this.AcceptRateResult();
}

private void AcceptRateResult()
{
    RateSelected = this.rateList[SelectedRateItemIndex].RateValue.Value;

    if (this.RateSelected == null && this.RateSelected == rateList[3].RateValue)
    {
        return;
    }

    // OnPropertyChanged();
    this.RaisePropertyChanged(nameof(RateSelected));
    this.RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
}

我期望DialogService.showDialog从对话框窗口返回索引值或项目值,以便可以将值分配给正在调用函数的主视图的标签。我已传递DialogName和参数,这将帮助我检索值,但它没有正常工作。
MainViewModel.cs

protected IDialogService dialogService;

public event Action<IDialogResult> RequestClose;

private int? rateselected;

public int? RateSelected
{
    get
    {
        rateselected = rateList[30].RateValue;
        return rateselected;
    }
    set => this.SetProperty(ref this.rateselected, value);
}

public int? PulseWidthSelected
{
    get 
    {
        var pulsewidth = pulsewidthList[4].PulseWidthValues;
        return pulsewidth;
    }
}

private static AssignDatabaseDataContext _adpDC = new AssignDatabaseDataContext();
private  ObservableRateList rateList;
private readonly ObservablePulseWidthList pulsewidthList;

private readonly ObservableCollection<RateTable> rateTables;
private readonly ObservableCollection<PulseWidthTable> pulseWidthTables;

public ICommand PulseWidthDialogCommand { get; set; }
public ICommand RateDialogCommand { get; set; }

public PulseWidthRateViewModel(IDialogService dialogservice)
{
    rateList = new ObservableRateList(_adpDC);
    pulsewidthList = new ObservablePulseWidthList(_adpDC);
    this.RaisePropertyChanged(nameof(RateSelected));
    this.dialogService = dialogservice;
            
    //Convert.ToInt32(this.rateTables.Where(i => i.RateValue == 50));
    this.PulseWidthDialogCommand = new DelegateCommand(this.ShowPulseWidthDialog);
    this.RateDialogCommand = new DelegateCommand(this.ShowRateDialog);
    this.pulseWidthTables = new ObservableCollection<PulseWidthTable>();
    this.rateTables = rateList;

    //Convert.ToInt32(rateTables.Select(s => s.RateValue == 50));//this.rateList[30];
}

private void ShowPulseWidthDialog()
{
    // var parameters = new DialogParameters { { } };
}

private void ShowRateDialog()
{
    if (RateSelected == null && RateSelected == rateList[30].RateValue)
    {
        return;
    }

    var currentValue = this.RateSelected ?? 0;

    this.RaisePropertyChanged(nameof(currentValue));

    var parameters = new DialogParameters {
                { DialogNames.RateSelected, currentValue },
                { DialogNames.RateDialogList, rateTables }
            };

    dialogService.ShowDialog(DialogNames.RateSelectDialog, parameters, null);
    //this.dialogService.ShowDialog(dia);
}
dced5bon

dced5bon1#

dialogService.ShowDialog(DialogNames.RateSelectDialog, parameters, null);
ShowDialog不将结果作为返回值返回,而是将其传递给回调函数(null)。
如果提供一个实际的回调,你将从对话框中接收结果,并可以将其传递给ShowDialog的调用者,例如通过闭包。
例如:

// returning the result
this.RequestClose?.Invoke(new DialogResult(ButtonResult.OK, new DialogParameters { { "selectedRateValue", selectedratevalue.Value } }));

// receiving the result
int selectedRateValue;
dialogService.ShowDialog(DialogNames.RateSelectDialog, parameters, result => result.Parameters.TryGetValue( "selectedRateValue", out selectedRateValue) );

相关问题