WPF ComboBox itemsSource 在 Prism MVVM 中不起作用

WPF ComboBox itemsSource not working in Prism MVVM

我正在尝试从数据库中获取项目列表并将其填充到 ComboBox 中,使用 EntityFramework 和棱镜 MVVM,

it is populating the items but the text is not appeared when I open the ComboBox? did I do something wrong in the below code.

Is this the right way to implement the MVVM Pattern?


请在下面找到 UPDATE1:

代码隐藏:

public MainWindow()
{
    InitializeComponent();
    this.DataContext = new XViewModel();
 }

型号:

public class SpModel
{
    public string SpName { get; set; }
    public string SpID { get; set; }
}

视图模型:

public class XViewModel : BindableBase
{
    private List<SpModel> _spList;
    public List<SpModel> SpList
    {
        get { return _spList; }
        set { SetProperty(ref (_spList), value); }
    }
}

public XViewModel()
{
    FillDefaultData();
}

private void FillDefaultData()
{
    using (JContext dc = new JContext())
    {
        var query = (from sp in dc.Sps
                     select new SpModel()
                     {
                         SpID = sp.SpID.ToString(),
                         SpName = sp.SpName
                     }).ToList();

        if (query != null && query.Count() > 0)
            SpList = query;
    }
}

XAML:

<ComboBox x:Name="ddlSp" Height="38" Padding="2"
          ItemsSource="{Binding SpList}"                                                     
          SelectedValuePath="SpID"          
          DisplayMemberPath="SpName" />

更新 1

风格:

<Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                <Border Name="Border"
                        SnapsToDevicePixels="true">
                    <ContentPresenter 
                                      TextBlock.FontSize="14"
                                      TextBlock.Foreground="Black"
                                      TextBlock.FontWeight="Bold">
                        <ContentPresenter.Content>
                            <Grid>

这一行的问题:

<TextBlock Text="{Binding Content}" />

..

                            </Grid>
                        </ContentPresenter.Content>
                    </ContentPresenter>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsHighlighted" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="#B1B1B1"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

您的代码正确且有效。在这种情况下绝对不需要 ObservableCollection 。我相信您认为 ComboBox 是空的,因为您没有选择。尝试添加这个,你应该在分配 SpList:

时看到第一个条目
<ComboBox ... SelectedIndex="0" />

您不应该在 ControlTemplate:

中设置 ContentPresenterContent 属性
<Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                <Border Name="Border" SnapsToDevicePixels="true">
                    <ContentPresenter 
                                      TextBlock.FontSize="14"
                                      TextBlock.Foreground="Black"
                                      TextBlock.FontWeight="Bold" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsHighlighted" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="#B1B1B1"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>