c# - 组合框的默认文本

标签 c# wpf xaml combobox

我想要在我的组合框中有一个默认文本,因为我已将组合框绑定(bind)到某些项目列表...这是我的 xaml 文件代码。

<ComboBox x:Name="ProjectComboBox" 
          Text="{Binding ProjectNameBinding}" 
          ItemsSource="{Binding projectList, ElementName=MainWin}"
          SelectedValuePath="_id" DisplayMemberPath="_name"  
          SelectedItem="{Binding ProjectNameBindingClass, Mode=OneWayToSource}" 
          Width="130" Background="White" BorderThickness="1"  
          FontFamily="/TimeSheet;component/Resources/#Open Sans" FontSize="12" 
          Canvas.Right="159" Canvas.Top="8" Height="47">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding _name}" TextWrapping="Wrap"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

最佳答案

您尝试过文本属性

<ComboBox x:Name="ProjectComboBox"
  IsEditable=True
  Text="{Binding ProjectNameBinding}" ....../>

您可以使用 SelectedIndex 属性并将其设置为 0(SelectedIndex=0),这会显示给定源中的第一项。

您可以像链接 How to display default text "--Select Team --" in combo box on pageload in WPF? 中那样执行此操作

关于c# - 组合框的默认文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22558536/

相关文章:

c# - 根据字数过滤字符串

c# - 从实现类调用 C# 接口(interface)默认方法

WPF,让垂直拉伸(stretch)按预期工作!

c# - Windows 8 应用程序/加载屏幕

c# - 监视文件夹并查找文件是否在 Windows 应用程序中打开

c# - wpf中的椭圆形按钮

.net - 我在哪里可以获得线程安全的 CollectionView?

wpf - 根据多个属性设置可见性

c# - 使用 XAML 中的 ReswFileCodeGenerator 从 .resw 文件绑定(bind)字符串

c# - 使用带有 C# 的 XAML 为 Windows Phone 8 应用程序设置计时器