【发布时间】:2013-02-18 09:24:42
【问题描述】:
我们有一个 ComboBox 的样式,例如:
<Style x:Key="OurComboBox" TargetType="ComboBox">
<!-- omitted style Properties -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<ToggleButton Name="ToggleButton"
Grid.Column="2"
ClickMode="Press"
Focusable="false"
IsChecked="{Binding Path=IsDropDownOpen,
Mode=TwoWay,
RelativeSource={RelativeSource TemplatedParent}}"
Style="{StaticResource ComboBoxToggleButton}" />
<ContentPresenter Name="ContentSite"
Margin="3,3,23,3"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
IsHitTestVisible="False" />
<TextBox x:Name="PART_EditableTextBox"
Margin="5,3,23,1"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Background="Transparent"
Focusable="False"
FontFamily="Arial Narrow"
FontWeight="DemiBold"
Foreground="#FF404040"
IsReadOnly="True"
PreviewMouseDown=""
Style="{x:Null}"
Template="{StaticResource ComboBoxTextBox}"
Text="{TemplateBinding Text}"
Visibility="Hidden" />
<!-- omitted PopUp and ControlTemplate.Triggers -->
在此基础上,我们有了另一种更具体的风格
<Style x:Key="comboBoxSpecialPage"
BasedOn="{StaticResource OurComboBox}"
TargetType="ComboBox">
<Style.Triggers>
<Trigger Property="SelectedIndex" Value="-1">
<Setter Property="Text" Value="Select value" />
</Trigger>
</Style.Triggers>
</Style>
如果在 ComboBox 中未选择任何内容,则会导致文本“选择值”,例如在应用程序启动时。
但是当我直接点击 TextBox 文本时,什么也没有发生。
所以问题是: 如何实现 PopUp 已打开,就像其余部分一样 ComboBox(没有Text的部分)被点击了?
-编辑- 如果我省略了有趣的部分,请告诉我,我会添加它们。
【问题讨论】:
-
你真的要实现这样的行为吗?关于可用性,这是一个奇怪的解决方案,因为您有一个用户操作和两个期望的行为。如果我使用
ComboBox,我希望选择我单击的项目。我不希望TextBox切换到编辑模式。 -
我希望 ComboBox 表现得像一个 ComboBox。我不希望 TextBox 部分是可编辑的,它应该显示一些文本。如果在 TextBox 中单击,它应该会弹出 ComboBox。现在,它没有。如果单击文本未覆盖的区域,则会弹出它。如何让它在点击文本时弹出?
-
对不起,我遇到了你的问题。
标签: c# wpf xaml combobox textbox