【问题标题】:WPF Editable Combobox does not change SelectedValue while typingWPF 可编辑组合框在键入时不会更改 SelectedValue
【发布时间】:2014-08-21 07:51:00
【问题描述】:

我定义了一个 Combobox,它的 SelectedValue 绑定到视图模型 VM.SelectedServiceTypeId 上的一个属性

<ComboBox Name="ServiceTypeComboBox"
          IsEditable="True"
          Grid.Row="1"
          Grid.Column="1"
          Margin="5"
          DisplayMemberPath="ServiceTypeName"
          ItemsSource="{Binding ServiceTypes,Mode=TwoWay}"
          SelectedValue="{Binding SelectedServiceTypeId, Mode=TwoWay}"
          SelectedValuePath="ServiceTypeId" 
          Loaded="ServiceTypeComboBox_Loaded"
          />

当用户在下拉菜单中选择一个项目时,该值会正确更新,但会导致组合框 IsEditable 用户能够键入他想要的任何内容,这不是 ItemSource 中的值。在这种情况下,SelectedValue 不会改变。

我需要做的是当 SelectedValue 在 ItemsSource 中时启用一个按钮。

你有什么提示吗?

【问题讨论】:

    标签: wpf combobox


    【解决方案1】:

    您的 wpf 组合框有一个属性“Text”,其中包含用户输入的文本。您必须编写一些代码来检查输入的文本是否与 itemssource 中的任何内容匹配。

    【讨论】:

      【解决方案2】:
      <Button Content="Click">
              <Button.Style>
                  <Style TargetType="Button">
                      <Setter Property="IsEnabled" Value="True"/>
                      <Style.Triggers>
                          <DataTrigger Binding="{Binding ElementName=ServiceTypeComboBox, Path=SelectedValue}" Value="{x:Null}">
                              <Setter Property="IsEnabled" Value="False"/>
                          </DataTrigger>
                      </Style.Triggers>
                  </Style>
              </Button.Style>
          </Button>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多