我正在尝试将我的comboBox的selectedItem设置为一个名为SelectedSimulationCase的属性。当我尝试从_allData.SimulationCase获取此值时,我遇到了一个问题。当我设置一个断点时,一切都表明它已正确设置,但仍然显示空白。如果我使用CurrentSimulationCase [0]初始化SelectedSimulationCase属性,它将正确显示。
我试图将selectedItem更改为SelectedValue。
我的观点
<UserControl x:Class="HysysLNG.Desktop.Views.SelectSimulationCaseView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="400"
Width="850">
<Grid
HorizontalAlignment="Left"
Height="400"
Width="850"
RenderTransformOrigin="0.478,0.575" Margin="0,0,-0.4,0" VerticalAlignment="Top">
<Button
</Button>
<Label
</Label>
<ComboBox
HorizontalAlignment="Left"
Margin="75,75,0,0"
VerticalAlignment="Top"
Width="700"
ItemsSource="{Binding CurrentSimulationCases}"
SelectedItem="{Binding Path=SelectedSimulationCase, Mode=TwoWay}"
DisplayMemberPath="FullName">
</ComboBox>
</Grid>
</UserControl>
我的视图模型
using System;
using HysysLNG.Desktop.Models;
using HysysLNG.Desktop.Relay;
using HysysLNG.Desktop.Repository.StreamRepo;
using System.Collections.Generic;
using System.Windows.Input;
using SimulationCase = HysysLNG.Desktop.Models.SimulationCase;
namespace HysysLNG.Desktop.Views
{
public class SelectSimulationCaseViewModel : BaseViewModel, IPageViewModel
{
private readonly AllData _allData;
public SelectSimulationCaseViewModel(IStreamRepository repo, AllData allData)
{
_allData = allData;
CurrentSimulationCases = repo.GetSimulationCases();
SelectedSimulationCase = _allData.SimulationCase ?? CurrentSimulationCases[0];
}
public IList<SimulationCase> CurrentSimulationCases { get; set; }
public SimulationCase SelectedSimulationCase { get; set; }
public ICommand GoToSelectStreamScreen => new RelayCommand(x =>
{
_allData.SimulationCase = SelectedSimulationCase;
Mediator.Notify("NavigateTo", PageName.SelectStreams);
});
}
}
我希望视图模型中的SelectedSimulationCase的值始终显示在视图中。