MVVM Caliburn.micro 组合框 Country-City 无法正常工作
MVVM Caliburn.micro comboboxes Country-City doesn't work properly
我将 MVVM 与 Caliburn.Micro 一起使用,但我遇到了问题。
所以我有 2 个组合框。第一个是代表国家列表,第二个是代表城市。我希望每当第一个列表中的国家发生变化时,城市列表就会更新,并带有相应的城市列表。我的问题是城市列表没有更新。
这是我的代码:
public class MyViewModel : PropertyChangedBase
{
Company company = new Company();
List<string> countries = new List<string> {"USA","Germany" };
public string Name
{
get { return company.name; }
set { company.name = value;
}
}
public List<string> Countries
{
get { return countries; }
set {
company.country = ToString();
NotifyOfPropertyChange(() => Countries);
NotifyOfPropertyChange(() => Cities);
}
}
public List<string> Cities
{
get {
switch (company.country)
{
case "USA": return new List<string> { "New York", "Los Angeles" };
case "Germany": return new List<string> { "Hamburg", "Berlin" };
default: return new List<string> { "DEFAULT", "DEFAULT" };
}
}
set { company.city = value.ToString();
NotifyOfPropertyChange(() => Cities);
}
}
}
现在城市列表保留默认成员(DEFAULT、DEFAULT)。该视图仅包含 2 个具有相应名称的组合框:
<Grid>
<ComboBox x:Name="Countries" />
<ComboBox x:Name="Cities" />
</Grid>
一些建议?
抱歉我的英语不好。
将国家 ComboBox
的 SelectedItem
属性 绑定到视图模型的来源 属性:
<ComboBox x:Name="Countries" ItemsSource="{Binding Countries}" SelectedItem="{Binding SelectedCountry}" />
<ComboBox x:Name="Cities" ItemsSource="{Binding Cities}" />
...并在 setter 中为 Cities
属性 引发 PropertyChanged
事件:
public class MyViewModel : PropertyChangedBase
{
Company company = new Company();
List<string> countries = new List<string> { "USA", "Germany" };
public string SelectedCountry
{
get { return company.country; }
set
{
company.country = value;
NotifyOfPropertyChange(() => SelectedCountry);
NotifyOfPropertyChange(() => Cities);
}
}
public List<string> Countries
{
get { return countries; }
set
{
countries = value;
NotifyOfPropertyChange(() => Countries);
NotifyOfPropertyChange(() => Cities);
}
}
public List<string> Cities
{
get
{
switch (company.country)
{
case "USA": return new List<string> { "New York", "Los Angeles" };
case "Germany": return new List<string> { "Hamburg", "Berlin" };
default: return new List<string> { "DEFAULT", "DEFAULT" };
}
}
}
}
请参阅以下博客 post 以获得完整示例以及有关如何实现这种级联的更多信息 ComboBoxes
:https://blog.magnusmontin.net/2013/06/17/cascading-comboboxes-in-wpf-using-mvvm/
我将 MVVM 与 Caliburn.Micro 一起使用,但我遇到了问题。 所以我有 2 个组合框。第一个是代表国家列表,第二个是代表城市。我希望每当第一个列表中的国家发生变化时,城市列表就会更新,并带有相应的城市列表。我的问题是城市列表没有更新。 这是我的代码:
public class MyViewModel : PropertyChangedBase
{
Company company = new Company();
List<string> countries = new List<string> {"USA","Germany" };
public string Name
{
get { return company.name; }
set { company.name = value;
}
}
public List<string> Countries
{
get { return countries; }
set {
company.country = ToString();
NotifyOfPropertyChange(() => Countries);
NotifyOfPropertyChange(() => Cities);
}
}
public List<string> Cities
{
get {
switch (company.country)
{
case "USA": return new List<string> { "New York", "Los Angeles" };
case "Germany": return new List<string> { "Hamburg", "Berlin" };
default: return new List<string> { "DEFAULT", "DEFAULT" };
}
}
set { company.city = value.ToString();
NotifyOfPropertyChange(() => Cities);
}
}
}
现在城市列表保留默认成员(DEFAULT、DEFAULT)。该视图仅包含 2 个具有相应名称的组合框:
<Grid>
<ComboBox x:Name="Countries" />
<ComboBox x:Name="Cities" />
</Grid>
一些建议? 抱歉我的英语不好。
将国家 ComboBox
的 SelectedItem
属性 绑定到视图模型的来源 属性:
<ComboBox x:Name="Countries" ItemsSource="{Binding Countries}" SelectedItem="{Binding SelectedCountry}" />
<ComboBox x:Name="Cities" ItemsSource="{Binding Cities}" />
...并在 setter 中为 Cities
属性 引发 PropertyChanged
事件:
public class MyViewModel : PropertyChangedBase
{
Company company = new Company();
List<string> countries = new List<string> { "USA", "Germany" };
public string SelectedCountry
{
get { return company.country; }
set
{
company.country = value;
NotifyOfPropertyChange(() => SelectedCountry);
NotifyOfPropertyChange(() => Cities);
}
}
public List<string> Countries
{
get { return countries; }
set
{
countries = value;
NotifyOfPropertyChange(() => Countries);
NotifyOfPropertyChange(() => Cities);
}
}
public List<string> Cities
{
get
{
switch (company.country)
{
case "USA": return new List<string> { "New York", "Los Angeles" };
case "Germany": return new List<string> { "Hamburg", "Berlin" };
default: return new List<string> { "DEFAULT", "DEFAULT" };
}
}
}
}
请参阅以下博客 post 以获得完整示例以及有关如何实现这种级联的更多信息 ComboBoxes
:https://blog.magnusmontin.net/2013/06/17/cascading-comboboxes-in-wpf-using-mvvm/