Silverlight ListboxItem可见性绑定

时间:2011-05-10 13:52:19

标签: silverlight-4.0 expression-blend

我正在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<SitesItem>类型。 SiteItem具有属性IsRegisteredIsFavorite

所以我想问题是:是否有可能绑定到IsRegistered中的IsFavoriteItemContainerStyle

1 个答案:

答案 0 :(得分:0)

WPF已经附带了一个开箱即用的BooleanToVisibilityConverter。所以只需按如下方式定义:

<UserControl.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</UserControl.Resources>

希望这有帮助