Silverlight 4 中的列表框禁用状态
所以我正在设计一个列表框,当列表框被禁用时,我需要做一个灰色的样式。然而,当我查看 Blend 中的状态选项卡时,只存在验证状态 - 没有包括禁用状态在内的常见常见状态的迹象。
我尝试创建一个没有自定义样式、只有一个列表框的普通项目,但同样的事情发生了。我的问题是,如何为列表框设置禁用状态的样式?我错过了一些明显的东西吗?
So I'm styling a ListBox and I've got to the part where I need to do a greyed out style when the ListBox is disabled. However when I look a the states tab in Blend, there's only Validation States present - no sign of the usual Common States which include the Disabled state.
I tried creating a vanilla project with no custom styles and just a ListBox and the same thing happens. My question is, how do I go about styling a disabled state for a ListBox? Am I missing something obvious??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先尝试简单的方法:编辑ListBoxItem模板,而不是列表框。这是以禁用状态显示的项目,而不是列表框。
在混合中:
“编辑附加模板”> “编辑生成的项目容器(ItemContainerStyle)”>编辑副本。
作为测试,我在禁用状态下强制背景颜色为红色(见下图)。背景颜色通常源自父列表。 XAML 太大,无法在此处列出。
列表框中的项目容器由包含 3 个矩形(提供边框颜色效果)的网格和用于保存实际项目内容的内容容器组成。
明显的问题...项目下的所有空白。呸!一定是更好的办法。
现在尝试更改 ListBox 模板:
要更改 ListBox 本身的模板,我认为您可以将 ListView 模板中滚动查看器的背景颜色绑定到控件的 IsEnabled 属性。这将需要一个自定义值转换器(将 IsEnabled bool? 转换为 Brush 对象),但它们的创建非常简单。
TemplateBinding 不支持转换器,但我发现您如果您使用相对源,则可以在模板中使用普通绑定:
结果如下所示:
First tried the simple approach: Edit the ListBoxItem template, rather than the List box. It is the items that are displayed in disabled state, not the listbox.
In blend:
"Edit Additional Templates" > "Edit Generated Item Container (ItemContainerStyle)" > Edit a copy.
As a test I forced the background colour to red in the disabled state (see picture below). The background colour is normally derived from the parent list. The XAML is too big to list here.
An item container in a listbox consists of a grid containing 3 rectangles (to give the border colour effects) and a content container to hold the actual item content.
Obvious problem... all the white-space under the items. Bah! Must be a better way.
Now try to change the ListBox template instead:
To change the template of the ListBox itself I thought you might be able to bind the background colour of the scrollviewer within the ListView Template to the IsEnabled property of the control. This would require a custom value converter (to convert the IsEnabled bool? to a Brush object), but they are pretty simple to create.
TemplateBinding does not support a convertor, but I found that you can use a normal binding in a template, if you use a RelativeSource:
The result looked like this:
ListBox 是一个嵌套控件。
您可能必须设置保存 ListBoxItem 的 ScrollViewer 控件的样式。
以下两个链接解释了如何设置列表框的样式。它们并不能直接回答您的问题,但可以让您了解其工作原理。
http://www.codeproject.com/KB/expression/ListBoxStylingSilverlight.aspx
http://www.codeproject.com/KB/expression/ListBoxStylingPart2.aspx
ListBox is a nested control.
You probably will have to style the ScrollViewer control that holds the ListBoxItem(s).
The following two links explain how to style a ListBox. They are not a direct answer to your question, but they may give you some insight on how it works.
http://www.codeproject.com/KB/expression/ListBoxStylingSilverlight.aspx
http://www.codeproject.com/KB/expression/ListBoxStylingPart2.aspx