嘿,
我现在的程序中有一个小问题,这比我应该更困扰。
这是我在程序中使用的单身人士。
public class Resources
{
private static Resources _instance;
public static Resources Instance
{
get { return _instance ?? (_instance = new Resources()); }
}
#region Properties
public Candidate Candidate { get; set; }
public Enterprise Enterprise { get; set; }
public WikomContact WikomContact { get; set; }
public AMSContact AMSContact { get; set; }
#endregion
}
我使用这些候选/企业/...-对象作为我的组合的项目,这很好。要在其他ViewModels中使用这些对象,我想将它们保存到Singleton级,从那里我想将它们加载到所有其他ViewModels中。
public _0InfoTalkViewModel()
{
_context = new WikomContext();
RefreshViewCommand = new RelayCommand(RefreshView);
PrintViewCommand = new RelayCommand(PrintView);
SaveViewCommand = new RelayCommand(SaveView);
EnterpriseIsEnabled = false;
WikomContactIsEnabled = true;
Candidate = Resources.Instance.Candidate;
Enterprise = Resources.Instance.Enterprise;
WikomContact = Resources.Instance.WikomContact;
}
这是我在程序中使用的组合之一。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="170" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Content="Enterprise" Margin="3" Foreground="White" Grid.Column="0"/>
<ComboBox ItemsSource="{Binding EnterpriseList, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedItem="{Binding Enterprise, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
DisplayMemberPath="Name"
IsEditable="True"
IsEnabled="{Binding EnterpriseIsEnabled, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Margin="3" Grid.Column="1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding RefreshViewCommand, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</Grid>
saveview-method看起来像
public void SaveView()
{
Resources.Instance.Candidate = Candidate;
Resources.Instance.Enterprise = Enterprise;
Resources.Instance.WikomContact = WikomContact;
}
加载表单后,还会触发刷新方法。问题是,组合蛋白从数据库中获取正确的输入,但是如果我选择其中一个,然后更改视图并再次加载,则选择的值不是我之前放置的...
有人知道我在做什么错吗?还是可以告诉我为什么我的"想法/方法"不起作用?
对不起,英语不好,我通常会说德语!;)
snaylor;
您的属性不应该是静态的,它们应该是实例属性:
public Candidate Candidate { get; set; }
public Enterprise Enterprise { get; set; }
public WikomContact WikomContact { get; set; }
public AMSContact AMSContact { get; set; }
当您引用单身人士时,资源应该是对实例的引用:
Candidate = Resources.Instance.Candidate;
Resources.Instance.Candidate = Candidate;