【问题标题】:Caliburn.Micro: Combobox is not populatedCaliburn.Micro:未填充组合框
【发布时间】:2013-05-20 12:35:34
【问题描述】:

我有一个带有 Caliburn.Micro 的 WPF 应用程序。我正在尝试填充我刚刚添加的新组合框,但它保持为空,尽管同一视图上的其他组合框已填充。我进行了调试,所以我知道 ViewModel 中的 GeorgaphyNames 源集合已正确填充。你能帮忙吗?

这是我的代码:

型号:

public class GeographyName : PropertyChangedBase
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            if (_name == value) return;
            _name = value;
            NotifyOfPropertyChange(() => Name);
        }
    }
  ...
}

查看:

            <ComboBox Grid.Row="6" Grid.Column="1" Name="GeographyNames" DisplayMemberPath="Name" SelectedValuePath="Name" SelectedItem="SelectedGeographyName" Style="{StaticResource DetailCombo}" />

视图模型:

    private BindableCollection<GeographyName> _geographyNames;
    public BindableCollection<GeographyName> GeorgaphyNames
    {
        get { return _geographyNames; }
        private set
        {
            if (_geographyNames == value)
                return;
            _geographyNames = value;
            NotifyOfPropertyChange(() => GeorgaphyNames);
        }
    }


    private GeographyName _selectedGeographyName;
    public GeographyName SelectedGeographyName
    {
        get { return _selectedGeographyName; }
        set
        {
            if (_selectedGeographyName == value)
                return;
            _selectedGeographyName = value;
            NotifyOfPropertyChange(() => SelectedGeographyName);
            IsModified = true;
        }
    }

组合框在另一个组合框中更改选择时填充:

    private Ldc _selectedLdc;
    public Ldc SelectedLdc
    {
        get { return _selectedLdc; }
        set
        {
            if (_selectedLdc == value)
                return;
            _selectedLdc = value;
            NotifyOfPropertyChange(() => SelectedLdc);
            if (SelectedLdc != null)
            {
                GeorgaphyNames = GeographyName.GetData(SelectedLdc.LdcId);
            }
            IsModified = true;
        }
    }

【问题讨论】:

  • 从未与 Caliburn.micro 合作过,但您究竟在哪里设置组合框 ItemsSource?
  • 这是 Caliburn.Micro 中的约定。如果 BindableCollection 的名称与控件的名称相同,则将其用作其 ItemsSource。
  • 检查相关组合框的 DataContext。也许它在另一个元素中,它的 DataContext 未设置为您的 ViewModel。
  • 我不这么认为,因为我在这个旁边已经有其他组合框,并且它们填充正常。我看不出有什么区别...
  • 我刚刚编辑了问题以显示源集合的填充方式。

标签: c# wpf combobox caliburn.micro


【解决方案1】:

您的视图模型属性名为GeorgaphyNames,这是一个错字。

另外,最好在您的视图中使用x:Name,而不是Name。

【讨论】:

  • 是的!非常感谢!我一直在寻找和寻找......但仍然没有注意到它!
  • 为什么最好使用x:Name 而不是Name?
猜你喜欢
  • 2013-06-05
  • 1970-01-01
  • 1970-01-01
  • 2021-03-05
  • 1970-01-01
  • 2013-06-22
  • 2011-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多