所以我正在设计一个ListBox,我已经到了禁用ListBox时我需要做一个灰色样式的部分.但是,当我查看Blend中的状态选项卡时,只有Validation States存在 – 没有包含Disabled状态的常见Common States的迹象.
我尝试创建一个没有自定义样式的vanilla项目,只是一个ListBox,同样的事情发生了.我的问题是,我如何为ListBox设置禁用状态?我错过了一些明显的东西吗
解决方法
首先尝试了简单的方法:编辑ListBoxItem模板,而不是List框.它是以禁用状态显示的项目,而不是列表框.
混合:
“编辑其他模板”> “编辑生成的项目容器(ItemContainerStyle)”>编辑副本.
作为测试,我在禁用状态下将背景颜色强制为红色(见下图).背景颜色通常来自父列表. XAML太大了,无法在此列出.
列表框中的项容器由包含3个矩形的网格(用于提供边框颜色效果)和用于保存实际项目内容的内容容器组成.
> fillcolor
> fillcolor2
> contentPresenter
> FocusVisualElement
明显的问题……项目下的所有空白区域.呸!一定是更好的方式.
现在尝试更改ListBox模板:
要更改ListBox本身的模板,我认为您可以将ListView模板中的scrollviewer的背景颜色绑定到控件的IsEnabled属性.这需要一个自定义值转换器(将IsEnabled bool?转换为Brush对象),但它们很容易创建.
TemplateBinding不支持转换器,但我发现你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 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,System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }