WPF ComboBox 不显示选定的值
WPF ComboBox not displaying selected value
我已经成功地绑定了 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(非 material 设计),结果相同。
如果您需要,我会提供更多代码,但到目前为止,这个控件似乎刚刚损坏,无法正常工作。我可能遗漏了一些如何正确设置它的小细节。
编辑
视图模型:
public class WindowAddEmployeesViewModel : EmployeesViewModel, INotifyPropertyChanged
{
public ObservableCollection<PositionsViewModel> Positions { get; set; }
new public event PropertyChangedEventHandler PropertyChanged;
}
Base class 包含 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
使其可见,但我不希望用户能够编辑它。
您误解了 SelectedValue
的目的。您可以绑定到 SelectedValue
而不是 SelectedItem
。它与 ComboBox
显示的值无关。
显示的值可以通过在数据模型上将ItemsControl.DisplayMemberPath
设置为所需的属性来定义,但前提是ItemTemplate
未定义。 DisplayMemberPath
用于在简单情况下替换 DataTemplate
。
您显然想要设置 DisplayMemberPath
。
还有你当前的绑定
<ComboBox SelectedValue="{Binding Position}" .../>
不会解析(无论 ComboBox.IsEditable
的状态如何),因为 ComboBox
的 DataContext
显然是 WindowAddEmployeesViewModel
而不是 PositionsViewModel
.这可能是您使用 SelectedValue
错误的提示。
SelectedItem
:当前选择的数据模型。
SelectedValue
:returns 属性 在 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}" />
我已经成功地绑定了 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(非 material 设计),结果相同。
如果您需要,我会提供更多代码,但到目前为止,这个控件似乎刚刚损坏,无法正常工作。我可能遗漏了一些如何正确设置它的小细节。
编辑
视图模型:
public class WindowAddEmployeesViewModel : EmployeesViewModel, INotifyPropertyChanged
{
public ObservableCollection<PositionsViewModel> Positions { get; set; }
new public event PropertyChangedEventHandler PropertyChanged;
}
Base class 包含 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
使其可见,但我不希望用户能够编辑它。
您误解了 SelectedValue
的目的。您可以绑定到 SelectedValue
而不是 SelectedItem
。它与 ComboBox
显示的值无关。
显示的值可以通过在数据模型上将ItemsControl.DisplayMemberPath
设置为所需的属性来定义,但前提是ItemTemplate
未定义。 DisplayMemberPath
用于在简单情况下替换 DataTemplate
。
您显然想要设置 DisplayMemberPath
。
还有你当前的绑定
<ComboBox SelectedValue="{Binding Position}" .../>
不会解析(无论 ComboBox.IsEditable
的状态如何),因为 ComboBox
的 DataContext
显然是 WindowAddEmployeesViewModel
而不是 PositionsViewModel
.这可能是您使用 SelectedValue
错误的提示。
SelectedItem
:当前选择的数据模型。
SelectedValue
:returns 属性 在 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}" />