2

我正在将应用程序从 Windows Phone 7 移植到 Windows Phone 8,但 PathListBox 控件存在一些问题。

这是 XAML:

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
        <Path x:Name="path" Data="M58,382 C59,378 67,156 162,216 C257,276 268,381 325,268 C382,155 470,188 345,107.999 C220,27.9988 191,-10.0014 51,46.9988 C-89,103.999 -106,203.999 18,185.999 C142,167.999 108,105.999 179,130.999" HorizontalAlignment="Left" Height="363.298" Margin="4.98,54.202,0,0" Stretch="Fill" Stroke="Red" UseLayoutRounding="False" VerticalAlignment="Top" Width="475.02" StrokeThickness="3"/>

        <mec:PathListBox HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100">
            <mec:PathListBox.LayoutPaths>
                <mec:LayoutPath SourceElement="{Binding ElementName=path}"/>
            </mec:PathListBox.LayoutPaths>
            <mec:PathListBoxItem Content="PathListBoxItem" HorizontalAlignment="Left" Height="24" VerticalAlignment="Top" Width="100"/>
            <mec:PathListBoxItem Content="PathListBoxItem" HorizontalAlignment="Left" Height="24" VerticalAlignment="Top" Width="100"/>
            <mec:PathListBoxItem Content="PathListBoxItem" HorizontalAlignment="Left" Height="24" VerticalAlignment="Top" Width="100"/>
            <mec:PathListBoxItem Content="PathListBoxItem" HorizontalAlignment="Left" Height="24" VerticalAlignment="Top" Width="100"/>
        </mec:PathListBox>

    </Grid>

PathListBoxItems 不像在 WPF、Silverlight 和 Windows Phone 7 中那样遵循路径。这是什么原因?

没有编译错误或警告,Visual Studio 2012 没有给我任何警告。在 Blend 5 的属性面板中的 LayoutPaths 列表中,在移除“-”按钮旁边的“路径”项上有一个黄色的小警告符号。用鼠标悬停图标时的工具提示状态:

此对象不存在或者是此 PathListBox 的后代。

鉴于我提供的 XAML,这似乎不是真的。

我已经尝试过使用矩形、椭圆和直线。我已经更改了它们的声明顺序。没关系,Blend 5 总是给我同样的小警告。还有其他人在使用 Windows Phone 8 PathListBox 时遇到过这种情况吗?

4

2 回答 2

0

我已经猜到了。wp8 变体缺少控件模板。我将 wp7 中的 itemcontainer 样式复制到 wp8 应用程序的本地资源中,它现在似乎可以工作了;

<phone:PhoneApplicationPage.Resources>
    <Style x:Key="PathListBoxStyle1" TargetType="mec:PathListBox">
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="UseLayoutRounding" Value="False"/>
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <mec:PathPanel/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="mec:PathListBox">
                    <Grid>
                        <!--
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="ValidationStates">
                                <VisualState x:Name="Valid"/>
                                <VisualState x:Name="InvalidUnfocused">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="InvalidFocused">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" Storyboard.TargetName="validationTooltip">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <System:Boolean>True</System:Boolean>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        -->
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="2" Padding="{TemplateBinding Padding}">
                            <ItemsPresenter/>
                        </Border>
                        <!--
                        <Border x:Name="ValidationErrorElement" BorderBrush="#FFDB000C" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2" Visibility="Collapsed">
                            <ToolTipService.ToolTip>
                                <ToolTip x:Name="validationTooltip" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" Placement="Right" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Template="{StaticResource ValidationToolTipTemplate}">
                                    <ToolTip.Triggers>
                                        <EventTrigger RoutedEvent="Canvas.Loaded">
                                            <BeginStoryboard>
                                                <Storyboard>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsHitTestVisible" Storyboard.TargetName="validationTooltip">
                                                        <DiscreteObjectKeyFrame KeyTime="0">
                                                            <DiscreteObjectKeyFrame.Value>
                                                                <System:Boolean>true</System:Boolean>
                                                            </DiscreteObjectKeyFrame.Value>
                                                        </DiscreteObjectKeyFrame>
                                                    </ObjectAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </BeginStoryboard>
                                        </EventTrigger>
                                    </ToolTip.Triggers>
                                </ToolTip>
                            </ToolTipService.ToolTip>
                            <Grid Background="Transparent" HorizontalAlignment="Right" Height="10" Margin="0,-4,-4,0" VerticalAlignment="Top" Width="10">
                                <Path Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z" Fill="#FFDC000C" Margin="-1,3,0,0"/>
                                <Path Data="M 0,0 L2,0 L 8,6 L8,8" Fill="#ffffff" Margin="-1,3,0,0"/>
                            </Grid>
                        </Border>
                        -->
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>

    <DataTemplate x:Key="DataTemplate1">
        <Grid>
            <TextBlock Text="{Binding Name}" />
            <TextBlock Text="{Binding ElementName, RelativeSource={RelativeSource  Mode=TemplatedParent}}" />
        </Grid>
    </DataTemplate>
    <ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
        <mec:PathPanel/>
    </ItemsPanelTemplate>
    <mec:IsArrangedToScaleConverter x:Key="IsArrangedToScaleConverter"/>
    <Style x:Key="PathListBoxItemStyle1" TargetType="mec:PathListBoxItem">
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="mec:PathListBoxItem">
                    <Grid Background="{TemplateBinding Background}" RenderTransformOrigin="0.5,0.5">
                        <Grid.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform ScaleY="{Binding IsArranged, Converter={StaticResource IsArrangedToScaleConverter}, RelativeSource={RelativeSource TemplatedParent}}" ScaleX="{Binding IsArranged, Converter={StaticResource IsArrangedToScaleConverter}, RelativeSource={RelativeSource TemplatedParent}}"/>
                                <SkewTransform/>
                                <RotateTransform Angle="{Binding OrientationAngle, RelativeSource={RelativeSource TemplatedParent}}"/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </Grid.RenderTransform>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To=".35" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="contentPresenter"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected"/>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To=".75" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor2"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="FocusVisualElement">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unfocused"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Rectangle x:Name="fillColor" Fill="#FFBADDE9" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/>
                        <Rectangle x:Name="fillColor2" Fill="#FFBADDE9" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/>
                        <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"/>
                        <Rectangle x:Name="FocusVisualElement" RadiusY="1" RadiusX="1" Stroke="#FF6DBDD1" StrokeThickness="1" Visibility="Collapsed"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</phone:PhoneApplicationPage.Resources>

使用它;

   <mec:PathListBox x:Name="MyPathListBox" HorizontalAlignment="Left"
                             Height="100"
                             VerticalAlignment="Top"
                             Width="100"
                                Style="{StaticResource PathListBoxStyle1}"
                                ItemContainerStyle="{StaticResource PathListBoxItemStyle1}"
                             ItemsSource="{Binding Items}" 

                             >
                <mec:PathListBox.ItemTemplate>
                    <DataTemplate>
                    <Grid>
                        <TextBlock>bla</TextBlock>
                    </Grid>
                    </DataTemplate>
                </mec:PathListBox.ItemTemplate>
                <mec:PathListBox.LayoutPaths>
                    <mec:LayoutPath Orientation="OrientToPath" SourceElement="{Binding ElementName=path}" />
                </mec:PathListBox.LayoutPaths>
            </mec:PathListBox>
于 2013-02-17T21:51:38.010 回答
0

如果您使用代码而不是 xaml,它似乎会更好地工作,但即便如此,它仍然会忽略一些设置;

private PathListBox AttachPathListBoxToShape(ViewModel viewModel, IEnumerable itemSource, Shape shape, string dataTemplateKey, string itemsPanelTemplateKey)
    {

        DataTemplate dataTemplate = (DataTemplate)Application.Current.Resources[dataTemplateKey];
        ItemsPanelTemplate itemsPanelTemplate = (ItemsPanelTemplate)Application.Current.Resources[itemsPanelTemplateKey];

        PathListBox dynoListBox = new PathListBox();
        dynoListBox.ItemsSource = itemSource; 
        dynoListBox.ItemTemplate = dataTemplate;
        dynoListBox.ItemsPanel = itemsPanelTemplate;
        LayoutPath dynoPath = new LayoutPath();
        dynoPath.SourceElement = shape;
        dynoPath.Distribution = Distribution.Even;
        dynoPath.Orientation = Microsoft.Expression.Controls.Orientation.OrientToPath;
        dynoListBox.LayoutPaths.Add(dynoPath);
        return dynoListBox;
    }    
于 2012-12-06T11:30:45.580 回答