wpf - 当 WPF ComboBox 的选择为空时,它可以显示替代文本吗?

标签 wpf xaml combobox null

你好!

我希望我的 WPF ComboBox 在其数据绑定(bind)选择为 null 时显示一些替代文本。

View 模型具有预期的属性:

public ThingoSelectionViewModel : INotifyPropertyChanged {
    public ThingoSelectionViewModel(IProvideThingos) {
        this.Thingos = IProvideThingos.GetThingos();
    }

    public ObservableCollection<Thingo> Thingos { get; set; }

    public Thingo SelectedThingo { 
        get { return this.selectedThingo; }
        set { // set this.selectedThingo and raise the property change notification
    }

    // ...

}

View 以预期的方式将 XAML 绑定(bind)到 View 模型:

<ComboBox x:Name="ComboboxDrive" SelectedItem="{Binding Path=SelectedThingo}"
          IsEditable="false" HorizontalAlignment="Left" MinWidth="100" 
          IsReadOnly="false" Style="{StaticResource ComboboxStyle}"
          Grid.Column="1" Grid.Row="1" Margin="5" SelectedIndex="0">
    <ComboBox.ItemsSource>
        <CompositeCollection>
        <ComboBoxItem IsEnabled="False">Select a thingo</ComboBoxItem>
        <CollectionContainer 
            Collection="{Binding Source={StaticResource Thingos}}" />
        </CompositeCollection>
    </ComboBox.ItemsSource>
</ComboBox>

楔入顶部的ComboBoxItem是一种在顶部获取额外项目的方法。它是纯粹的 Chrome : View 模型保持纯粹和简单。只有一个问题:只要组合框的选择为空,用户就希望显示“选择一项”。

用户希望默认选择某个事物。他们希望看到一条消息,告诉他们选择一个事物。

我希望避免使用 ThingoWrapper 类污染 View 模型,该类的 ToString 方法返回“选择一个事物”(如果其 .ActualThingo 属性为 null,在填充 Thingos 时包装每个 Thingo,并找出某种方法来防止用户选择空的 Thingo .

有没有办法使用纯 XAML 或纯 XAML 和 View 代码隐藏类中的几行代码在 ComboBox 边界内显示“选择事物”?

最佳答案

您的 MVVM 要求有多严格?您可以在 View 中添加一些隐藏代码吗?

也许您可以将 ComboBox 包含在网格中,如下所示:

<Grid>
    <ComboBox x:Name="ComboBoxControl"
              SelectionChanged="ComboBoxControl_SelectionChanged"
              HorizontalAlignment="Left" VerticalAlignment="Top" 
              MinWidth="{Binding ElementName=UnselectedText, Path=ActualWidth}">
        <ComboBoxItem>One</ComboBoxItem>
        <ComboBoxItem>Two</ComboBoxItem>
        <ComboBoxItem>Three</ComboBoxItem>
    </ComboBox>
    <TextBlock IsHitTestVisible="False" 
               x:Name="UnselectedText" 
               HorizontalAlignment="Left" 
               Text="Select an option..." 
               VerticalAlignment="Top" Margin="4" 
               Padding="0,0,30,0" />
</Grid>

然后,在后面的代码中,在事件处理程序中插入一些逻辑:

Private Sub ComboBoxControl_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
    If ComboBoxControl.SelectedIndex = -1 Then
        UnselectedText.Visibility = Windows.Visibility.Visible
    Else
        UnselectedText.Visibility = Windows.Visibility.Hidden
    End If
End Sub

在 TextBlock 上设置 IsHitTestVisible="False" DependencyProperty 让鼠标事件通过,以便您可以单击 ComboBox,并在代码中将可见性设置为 Hidden -behind 使默认 ComboBox 的外观布局在提示文本隐藏时不会跳转。

关于wpf - 当 WPF ComboBox 的选择为空时,它可以显示替代文本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2901536/

相关文章:

c# - WinRT 应用程序 : ComboxBox and ListBox does not display anything when ItemsSource is List<Type>

c# - wpf Canvas 动态背景图像

c# - Windows 10 高 DPI 屏幕上的 WPF 应用程序模糊

c# - 构建我的应用程序,创建可执行文件和安装程序

xaml - Bing map 可调整大小的多边形

c# - 如何在 WPF 组合框中找到插入符位置?

c# - 如何使用 ContentStringFormat 显示百分比值?

wpf - 在 DynamicResource 上使用 BasedOn 样式属性

WPF 嵌套用户控件绑定(bind)

在 for 循环中创建的组合框中的 python tkinter 引用