Silverlight ListboxItem 可见性绑定

发布于 2024-11-06 22:27:40 字数 6410 浏览 0 评论 0原文

我正在 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.ObservableCollectionSiteItem 具有属性 IsRegisteredIsFavorite

所以我想问题是:是否可以在 ItemContainerStyle 中绑定到 IsRegisteredIsFavorite

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 技术交流群。

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

发布评论

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

评论(1

李白 2024-11-13 22:27:40

WPF 已经附带了一个开箱即用的 BooleanToVisibilityConverter。所以简单地定义如下:

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

希望这有帮助

WPF already comes with a BooleanToVisibilityConverter out of the box. So simply define it as follows:

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

Hope this helps

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