WPF 无法在 ComboBox 中选择启用的项目

我正在尝试创建一个组合框,其中启用了选择列表中的某些项目而禁用了其他项目。在视觉上,我能够做到这一点,但是当我选择一个视觉上启用的项目时,什么也没有发生。


XAML:


<ComboBox ItemsSource="{Binding EndpointModel.DisplayFormat}" 

          VerticalAlignment="Center" Margin="0,0,10,0" 

          SelectedItem="{Binding EndpointModel.SelectMediaFormat}">

     <ComboBox.ItemContainerStyle>

          <Style TargetType="ComboBoxItem">

              <Setter Property="IsEnabled" Value="{Binding IsEnabled}" />

          </Style>

     </ComboBox.ItemContainerStyle>

     <ComboBox.ItemTemplate>

          <DataTemplate>

              <ComboBoxItem>

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

              </ComboBoxItem>

          </DataTemplate>

     </ComboBox.ItemTemplate>

</ComboBox>

http://img4.mukewang.com/637083c60001c48701900094.jpg

当我单击上面的选定项目时,什么也没有发生 - EndpointModel.SelectMediaFormat 没有执行。


删除ComboBox.ItemTemplate节的那一刻,我可以成功选择任何启用的项目,即


<ComboBox ItemsSource="{Binding EndpointModel.DisplayFormat}" 

          VerticalAlignment="Center" Margin="0,0,10,0" 

          SelectedItem="{Binding EndpointModel.SelectMediaFormat}">

     <ComboBox.ItemContainerStyle>

          <Style TargetType="ComboBoxItem">

              <Setter Property="IsEnabled" Value="{Binding IsEnabled}" />

          </Style>

     </ComboBox.ItemContainerStyle>

</ComboBox>

http://img4.mukewang.com/637083d30001597103770096.jpg

如您所见,我正在显示对象引用。我不知道如何用实际名称替换上面的内容。

正如您想象的那样,我在 WPF 还很陌生 :-)

任何建议,将不胜感激。


杨魅力
浏览 116回答 1
1回答

慕勒3428872

尝试使用该DisplayMemberPath属性Name(您要显示的属性)。<ComboBox ItemsSource="{Binding EndpointModel.DisplayFormat}"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VerticalAlignment="Center" Margin="0,0,10,0"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SelectedItem="{Binding EndpointModel.SelectMediaFormat}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DisplayMemberPath="Name">&nbsp; &nbsp; &nbsp;<ComboBox.ItemContainerStyle>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Style TargetType="ComboBoxItem">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="IsEnabled" Value="{Binding IsEnabled}" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Style>&nbsp; &nbsp; &nbsp;</ComboBox.ItemContainerStyle></ComboBox>
打开App,查看更多内容
随时随地看视频慕课网APP