wpf - WPF ComboBox itemsSource在Prism MVVM中不起作用

标签 wpf mvvm data-binding combobox prism

我正在尝试从数据库中获取项目列表,并使用ComboBox填充它
EntityFramework和Prism 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; }
}

ViewModel:
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>

最佳答案

您不应该在Content中设置ContentPresenterControlTemplate属性:

<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>

关于wpf - WPF ComboBox itemsSource在Prism MVVM中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50827711/

相关文章:

wpf - 如何使用新项目更新 listView?

c# - 绑定(bind) UserControl 的数据上下文

android - 无法解析 androidx.databinding :databinding-compiler:3. 5.3

java - 如何在 Controller 中使用 @RequestBody 将值映射到 DTO 时忽略 @JsonProperty 值

c# - 有没有办法避免在 Xamarin 中每个方法的 setter 中调用 NotifyChanged?

wpf - 如何使用带有 MVVM 模型的 WPF 数据绑定(bind)来使用自定义控件填充 StackPanel

c# - WPF:MVVM 和编辑分层数据

c# - WPF C# 输入框

wpf - WPF-组合框不在 ListView 中呈现(仅第一个)

wpf - 从 View 模型中选择数据网格行中的所有复选框