Silverlight 4 中的列表框禁用状态

发布于 2024-09-13 03:09:37 字数 177 浏览 16 评论 0原文

所以我正在设计一个列表框,当列表框被禁用时,我需要做一个灰色的样式。然而,当我查看 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遇见了你 2024-09-20 03:09:37

首先尝试简单的方法:编辑ListBoxItem模板,而不是列表框。这是以禁用状态显示的项目,而不是列表框。

在混合中:
“编辑附加模板”> “编辑生成的项目容器(ItemContainerStyle)”>编辑副本。

作为测试,我在禁用状态下强制背景颜色为红色(见下图)。背景颜色通常源自父列表。 XAML 太大,无法在此处列出。

列表框中的项目容器由包含 3 个矩形(提供边框颜色效果)的网格和用于保存实际项目内容的内容容器组成。

  • fillcolor
  • fillcolor2
  • contentPresenter
  • FocusVisualElement

示例输出

明显的问题...项目下的所有空白。呸!一定是更好的办法。

现在尝试更改 ListBox 模板:
要更改 ListBox 本身的模板,我认为您可以将 ListView 模板中滚动查看器的背景颜色绑定到控件的 IsEnabled 属性。这将需要一个自定义值转换器(将 IsEnabled bool? 转换为 Brush 对象),但它们的创建非常简单。

TemplateBinding 不支持转换器,但我发现您如果您使用相对源,则可以在模板中使用普通绑定

<ScrollViewer x:Name="ScrollViewer" BorderBrush="Transparent" BorderThickness="0" Background="{Binding IsEnabled, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource Bool2Color}}" Padding="{TemplateBinding Padding}" TabNavigation="{TemplateBinding TabNavigation}">

结果如下所示:

替代文字替代文字

The code for the value convertor is below

    public class BoolToColourConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool?)
            {
                return new SolidColorBrush((value as bool?).Value ? Colors.Red : Colors.Orange);
            }
            throw new NotImplementedException();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

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.

  • fillcolor
  • fillcolor2
  • contentPresenter
  • FocusVisualElement

Sample output

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:

<ScrollViewer x:Name="ScrollViewer" BorderBrush="Transparent" BorderThickness="0" Background="{Binding IsEnabled, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource Bool2Color}}" Padding="{TemplateBinding Padding}" TabNavigation="{TemplateBinding TabNavigation}">

The result looked like this:

alt textalt text

The code for the value convertor is below

    public class BoolToColourConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool?)
            {
                return new SolidColorBrush((value as bool?).Value ? Colors.Red : Colors.Orange);
            }
            throw new NotImplementedException();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
迷离° 2024-09-20 03:09:37

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文