“默认”模板化组合框的文本
我有一个基于数据模板的组合框,其中包含这样的复选框:
<ComboBox x:Name="cboComplex" Text="Select days...">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=IsSelected}" Width="20"/>
<TextBlock Text="{Binding DayOfWeek}" Width="100" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我遇到的问题是我希望组合框显示“选择日期...”,然后在单击时显示列表。不幸的是,设置 Text 属性似乎没有效果。任何想法或帮助将不胜感激。
提前致谢!
西格
I have a combo box that is based on a data template the includes check boxes like such:
<ComboBox x:Name="cboComplex" Text="Select days...">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=IsSelected}" Width="20"/>
<TextBlock Text="{Binding DayOfWeek}" Width="100" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The problem I'm having is that I'd like the combobox to display "Select days..." and then show the list when clicked. Unfortunately setting the Text property seems to have no effect. Any ideas or help would be greatly appreciated.
Thanks in advance!
Sieg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在组合框中显示默认文本
IsEditable = true
IsReadOnly = true
Show default text in combo box
IsEditable = true
IsReadOnly = true
您必须在基础集合类中创建一个新的 Item,其值为“Select Days...”,索引为 [0],并将所选索引更改为 0。
或者另一个选项是在其顶部放置一个标签文本“Select Days...”,然后监听 OnSelectionChanged 事件,如果 SelectedIndex 不为 -1,则将标签可见性更改为 false,否则为 true。例如
You will have to create a new Item in your underlying collection class with the value "Select Days...", with index[0] and change the selected index to 0.
Or the other option is to put a label on top of it with the text "Select Days...", and then listen to OnSelectionChanged event, and if the SelectedIndex is not -1, change the labels visibility to false, otherwise true. e.g.
当 ComboBox 可编辑时使用 Text 属性。您可以通过向 ControlTemplate 添加一个元素来设置默认的“选择”类型消息,该元素仅在没有选择时显示,然后消失。使用此方法,您无需担心修改集合或让用户尝试从列表中选取“选择”消息,因为它不是列表的一部分。我建议使用标签在每个实例或样式中设置消息,然后添加一个新的 TextBlock 将其显示到默认模板中:
然后您可以像这样使用它:
这是默认 Aero 的完整 Blend 生成的副本进行了更改的 ComboBox 模板。您还需要主题命名空间 (xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes; assembly=PresentationFramework.Aero") 和对PresentationFramework.Aero 程序集的引用:
The Text property is used when the ComboBox is editable. You can set a default "Select" type message by adding an element to the ControlTemplate that shows up only when there is no selection and then disappears. Using this method you don't need to worry about modifying your collection or having a user try to pick the "Select" message from the list because it isn't part of the list. I'd recommend using the Tag to set your message on each instance or in a Style and then adding a new TextBlock to display it into the default template:
Then you could use it like this:
Here's a complete Blend generated copy of the default Aero ComboBox template with the changes. You'll also need the theme namespace (xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero") and a reference to the PresentationFramework.Aero assembly: