Silverlight ListboxItem 可见性绑定
我正在 Expression Blend 4 中制作 Silverlight 应用程序的原型,并尝试根据它们所绑定的数据中的布尔值来显示/隐藏 ListBox/ComboBox 项目。我在网上找到了一个示例,表明这可以工作,但事实并非如此:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
xmlns:local="clr-namespace:PrototypeScreens"
mc:Ignorable="d"
x:Class="PrototypeScreens.Toolbar"
Width="640" Height="31">
<UserControl.Resources>
<local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<ComboBox HorizontalAlignment="Right" Style="{StaticResource ComboBox-Sketch}" VerticalAlignment="Top" Width="32" ItemsSource="{Binding Sites}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid>
<ed:RegularPolygon Fill="{StaticResource BaseBackground-Sketch}" ed:GeometryEffect.GeometryEffect="Sketch" HorizontalAlignment="Left" Height="10" InnerRadius="0.47211" Margin="0,0,0,0" PointCount="5" Stretch="Fill" Stroke="{StaticResource BaseBorder-Sketch}" StrokeThickness="2" UseLayoutRounding="False" Width="10"
Visibility="{Binding IsFavorite, Converter={StaticResource BooleanToVisibilityConverter}}" />
<TextBlock Style="{StaticResource BasicTextBlock-Sketch}" Text="{Binding Name}" HorizontalAlignment="Left" Margin="15,0,0,0" />
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
<!--
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="Visibility" Value="{Binding IsRegistered, Converter={StaticResource BooleanToVisibilityConverter}}" />
</Style>
</ComboBox.ItemContainerStyle>
-->
</ComboBox>
<ListBox Height="29" Margin="0,0,32,0" Style="{StaticResource ListBox-Sketch}" VerticalAlignment="Top" ItemsSource="{Binding Sites}" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" FlowDirection="RightToLeft" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Style="{StaticResource BasicTextBlock-Sketch}" Text="{Binding Id}" HorizontalAlignment="Left" Margin="0,0,0,0" />
<ed:RegularPolygon Fill="{StaticResource BaseBackground-Sketch}" ed:GeometryEffect.GeometryEffect="Sketch" HorizontalAlignment="Left" Height="10" InnerRadius="0.47211" Margin="20,0,0,0" PointCount="5" Stretch="Fill" Stroke="{StaticResource BaseBorder-Sketch}" StrokeThickness="2" UseLayoutRounding="False" Width="10"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<!--
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Visibility" Value="{Binding IsFavorite, Converter={StaticResource BooleanToVisibilityConverter}}" />
</Style>
</ListBox.ItemContainerStyle>
-->
</ListBox>
</Grid>
</UserControl>
当我尝试使用此用户控件加载屏幕时,应用程序会崩溃,除非我注释掉容器样式(如上面的代码所示)。我做错了什么?
注意: ComboBoxItem 内的多边形的可见性工作正常。
更新我包含了整个用户控件的xaml
更新我得到了一些错误详细信息:
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MDDC; .NET4.0C; .NET4.0E; BRI/2)
Timestamp: Tue, 10 May 2011 15:21:02 UTC
Message: Unhandled Error in Silverlight Application Exception has been thrown by the target of an invocation. at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Reflection.Assembly.CreateInstance(String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at Microsoft.Expression.Prototyping.Workspace.PlayerWindow.InstantiateScreen(String screen, Boolean showImmediately)
at Microsoft.Expression.Prototyping.Workspace.PlayerWindow.TransitionScreens(String from, String to)
at Microsoft.Expression.Prototyping.Navigation.NavigationViewModel.NavigateToScreen(String name, Boolean record)
Line: 1
Char: 1
Code: 0
URI: file:///D:/Projects/Expression/Prototype/Bin/Debug/Default.html
更新:我认为我取得了一些进展。看起来绑定无法解析。 ListBox 和 ComboBox 都有 ItemsSource="{Binding Sites}"
且 Sites
的类型为 System.Collections.ObjectModel.ObservableCollection
。 SiteItem
具有属性 IsRegistered
和 IsFavorite
。
所以我想问题是:是否可以在 ItemContainerStyle
中绑定到 IsRegistered
和 IsFavorite
?
I am making a prototype of a Silverlight application in Expression Blend 4 and I'm trying to show/hide ListBox/ComboBox items based on boolean values in the data they are bound to. I found an example online which suggested this would work but it doesn't:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
xmlns:local="clr-namespace:PrototypeScreens"
mc:Ignorable="d"
x:Class="PrototypeScreens.Toolbar"
Width="640" Height="31">
<UserControl.Resources>
<local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<ComboBox HorizontalAlignment="Right" Style="{StaticResource ComboBox-Sketch}" VerticalAlignment="Top" Width="32" ItemsSource="{Binding Sites}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid>
<ed:RegularPolygon Fill="{StaticResource BaseBackground-Sketch}" ed:GeometryEffect.GeometryEffect="Sketch" HorizontalAlignment="Left" Height="10" InnerRadius="0.47211" Margin="0,0,0,0" PointCount="5" Stretch="Fill" Stroke="{StaticResource BaseBorder-Sketch}" StrokeThickness="2" UseLayoutRounding="False" Width="10"
Visibility="{Binding IsFavorite, Converter={StaticResource BooleanToVisibilityConverter}}" />
<TextBlock Style="{StaticResource BasicTextBlock-Sketch}" Text="{Binding Name}" HorizontalAlignment="Left" Margin="15,0,0,0" />
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
<!--
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="Visibility" Value="{Binding IsRegistered, Converter={StaticResource BooleanToVisibilityConverter}}" />
</Style>
</ComboBox.ItemContainerStyle>
-->
</ComboBox>
<ListBox Height="29" Margin="0,0,32,0" Style="{StaticResource ListBox-Sketch}" VerticalAlignment="Top" ItemsSource="{Binding Sites}" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" FlowDirection="RightToLeft" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Style="{StaticResource BasicTextBlock-Sketch}" Text="{Binding Id}" HorizontalAlignment="Left" Margin="0,0,0,0" />
<ed:RegularPolygon Fill="{StaticResource BaseBackground-Sketch}" ed:GeometryEffect.GeometryEffect="Sketch" HorizontalAlignment="Left" Height="10" InnerRadius="0.47211" Margin="20,0,0,0" PointCount="5" Stretch="Fill" Stroke="{StaticResource BaseBorder-Sketch}" StrokeThickness="2" UseLayoutRounding="False" Width="10"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<!--
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Visibility" Value="{Binding IsFavorite, Converter={StaticResource BooleanToVisibilityConverter}}" />
</Style>
</ListBox.ItemContainerStyle>
-->
</ListBox>
</Grid>
</UserControl>
When I try to load the screen with this user control, the app crashes unless I comment out the container styles (as in above code). What am I doing wrong?
NOTE: The visibility of the polygon inside the ComboBoxItem works fine.
UPDATE I included the xaml for the entire usercontrol
UPDATE I got some error details:
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MDDC; .NET4.0C; .NET4.0E; BRI/2)
Timestamp: Tue, 10 May 2011 15:21:02 UTC
Message: Unhandled Error in Silverlight Application Exception has been thrown by the target of an invocation. at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Reflection.Assembly.CreateInstance(String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at Microsoft.Expression.Prototyping.Workspace.PlayerWindow.InstantiateScreen(String screen, Boolean showImmediately)
at Microsoft.Expression.Prototyping.Workspace.PlayerWindow.TransitionScreens(String from, String to)
at Microsoft.Expression.Prototyping.Navigation.NavigationViewModel.NavigateToScreen(String name, Boolean record)
Line: 1
Char: 1
Code: 0
URI: file:///D:/Projects/Expression/Prototype/Bin/Debug/Default.html
UPDATE: I think I made some progress. It looks like the binding can not be resolved. The ListBox and ComboBox have both ItemsSource="{Binding Sites}"
and Sites
is of type System.Collections.ObjectModel.ObservableCollection<SitesItem>
. SiteItem
has properties IsRegistered
and IsFavorite
.
So I guess the question is: Is it possible to bind to bind to IsRegistered
and IsFavorite
in a ItemContainerStyle
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WPF 已经附带了一个开箱即用的 BooleanToVisibilityConverter。所以简单地定义如下:
希望这有帮助
WPF already comes with a BooleanToVisibilityConverter out of the box. So simply define it as follows:
Hope this helps