答案 0 :(得分:0)
<强>更新强>
除了使用TextBlock和ComboBox并将它们相互叠加之外,没有直接的方法在WPF中实现。
<Grid HorizontalAlignment="Left" Height="21.545" VerticalAlignment="Top" Width="120.964" Margin="56.958,108.962,0,0">
<ComboBox IsEditable="True" Name="myComboBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120">
<!--Items Here-->
</ComboBox>
<TextBlock Text="Please Select" IsHitTestVisible="False" Foreground="Gray" x:Name="textBlock" HorizontalAlignment="Left" TextWrapping="Wrap" Margin="3.993,2.664,0,0">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Setters>
<Setter Property="Visibility" Value="Hidden" />
</Style.Setters>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Text, ElementName=myComboBox, Mode=OneWay}" Value=""/>
<Condition Binding="{Binding IsKeyboardFocusWithin, ElementName=myComboBox, Mode=OneWay}" Value="False" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Visibility" Value="Visible" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
对此感到抱歉。我以前误解了这个问题。这个答案适用于UWP。
我认为占位符文字正是您所寻找的。在MSDN文档中查看ComboBox.PlaceholderText property。