ListBox在Silverlight 4中禁用状态

Dar*_*o Z 5 silverlight styles itemtemplate expression-blend silverlight-4.0

所以我正在设计一个ListBox,我已经到了禁用ListBox时我需要做一个灰色样式的部分.但是,当我查看Blend中的状态选项卡时,只有Validation States存在 - 没有包含Disabled状态的常见Common States的迹象.

我尝试创建一个没有自定义样式的vanilla项目,只是一个ListBox,同样的事情发生了.我的问题是,我如何为ListBox设置禁用状态?我错过了一些明显的东西吗

Gon*_*ing 1

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

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

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

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

  • 填色
  • 填充颜​​色2
  • 内容呈现者
  • 焦点视觉元素

样本输出

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

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

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

<ScrollViewer x:Name="ScrollViewer" BorderBrush="Transparent" BorderThickness="0" Background="{Binding IsEnabled, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource Bool2Color}}" Padding="{TemplateBinding Padding}" TabNavigation="{TemplateBinding TabNavigation}">
Run Code Online (Sandbox Code Playgroud)

结果如下:

替代文本替代文本

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)