【问题标题】:WPF ComboBox not displaying selected valueWPF ComboBox不显示所选值
【发布时间】:2020-05-13 15:07:16
【问题描述】:

我已成功绑定 ItemsSource,ComboBox 让我可以选择每个选项,但我看不到选择了哪个选项。 ComboBox 只是空白。 XAML 代码:

<ComboBox
  Name="Position"
  Grid.Row="5"
  SelectedValue="{Binding Position}"
  ItemsSource="{Binding Positions}"
  Style="{StaticResource MaterialDesignComboBox}"
  Margin="15,10,15,10"
  FontSize="12"/>

尝试了基本的 ComboBox(非材料设计),结果相同。

如果您需要,我会提供更多代码,但到目前为止,该控件似乎刚刚损坏,无法正常工作。我可能遗漏了一些如何正确设置的小细节。

编辑

视图模型:

public class WindowAddEmployeesViewModel : EmployeesViewModel, INotifyPropertyChanged
{
    public ObservableCollection<PositionsViewModel> Positions { get; set; }

    new public event PropertyChangedEventHandler PropertyChanged;
}

基类包含 FirstName、LastName、Position 等内容。INotifyPropertyChanged 未实现,因为 Fody.PropertyChanged 为我做了。

PositionViewModel:

public class PositionsViewModel : INotifyPropertyChanged
{
    public string Position { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    public override string ToString()
    {
        return $"{Position}";
    }
}

编辑

IsEditable 切换为True 使其可见,但我不希望用户能够对其进行编辑。

【问题讨论】:

  • 请分享您的视图模型(或表示组合框绑定的数据上下文的类)。你也设置了DisplayMemberPathSelectedValuePath吗?
  • @PavelAnikhouski DisplayMemberPathSelectedValuePath 尚未设置。在所有指南中,我发现它默认工作。对我来说它是空白的。用户必须记住点击了什么。这很糟糕。
  • 选中的Position在哪里定义?
  • @PavelAnikhouski 正如我在EmployeesViewModel 中所说的那样。这是你能想象到的最通用的类​​,所以我决定不提供这么多代码。
  • 问题是您是否设置了 Position 对象,是否使用列表中的 Position 进行设置?如果不是,您可能必须覆盖 Equals 和 GetHashCode si,ComboBox 可以识别您要选择的项目

标签: c# wpf combobox


【解决方案1】:

您误解了SelectedValue 的用途。您可以绑定到SelectedValue 而不是SelectedItem。它与ComboBox 显示的值无关。

可以通过将ItemsControl.DisplayMemberPath 设置为数据模型上所需的属性来定义显示的值,但仅限于未定义ItemTemplate 时。 DisplayMemberPath 用于在简单场景中替换 DataTemplate

你显然想设置DisplayMemberPath
还有你当前的绑定

<ComboBox SelectedValue="{Binding Position}" .../> 

无法解析(无论ComboBox.IsEditable 的状态如何),因为ComboBoxDataContext 显然是WindowAddEmployeesViewModel 而不是PositionsViewModel。这可能暗示您使用 SelectedValue 错误。

SelectedItem:当前选择的数据模型。

SelectedValue:返回SelectedItem 上的属性值,由SelectedValuePath 定义。

SelectedValuePath:设置属性的路径,应该是SelectedItem上的SelectedValue。参数是string

DisplayMemberPath:设置每个数据模型的属性路径,用于显示ComboBox 中的项目。参数是string

数据模型

public class PositionsViewModel : INotifyPropertyChanged
{
    public string Label { get; set; }
    public string Position { get; set; }

    public override string ToString() => Position;
}

观点

<!-- Since DisplayMemberPath="Position" the ComboBox will show the value of the Position property as its items -->
<ComboBox x:Name="PositionComboBox"
          DisplayMemberPath="Position"
          SelectedValuePath="Label"
          ItemsSource="{Binding Positions}" />

<!-- 
  Displays the PositionsViewModel. Implicitly invokes PositionsViewModel.ToString().   
  The TextBox will therefore display the property value of `PositionsViewModel.Position`.
 -->
<TextBox Text="{Binding ElementName=PositionComboBox, Path=SelectedItem}" />

<!-- 
  Displays the SelectedValue of the ComboBox. This value is defined by ComboBox.SelectedValuePath.
  The TextBox will therefore display the property value of `PositionsViewModel.Label` 
-->
<TextBox Text="{Binding ElementName=PositionComboBox, Path=SelectedValue}" />

【讨论】:

  • 先生,您是救生员。添加DisplayMemberPath="Position" 解决了这个问题。有趣的是,指南完全跳过了那部分,这不是你自己能猜到的。
猜你喜欢
  • 1970-01-01
  • 2014-11-05
  • 1970-01-01
  • 2010-11-11
  • 2011-04-19
  • 1970-01-01
  • 2020-12-11
  • 2021-07-31
  • 1970-01-01
相关资源
最近更新 更多