Как добавить несколько Shapes.Path в ListView в WinRT XAML?

1

У меня есть элемент управления ListView в XAML, который имеет элементы Styled под кодом ниже (WinRT С#). Но я не могу отобразить "path_2", как показано ниже.

Изображение 174551

Как добавить несколько Shapes.Path в ListView в WinRT XAML?

[MainPage.xaml.cs]

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Shapes;

namespace AppListViewPath
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            object o;
            Application.Current.Resources.TryGetValue("PathCustomStyle", out o);

            Path path_1 = new Path()
            {
                Style = (Style)o
            };

            Path path_2 = new Path()
            {
                Style = (Style)o
            };

            this.listView_path.Items.Add(path_1); // show
            this.listView_path.Items.Add(path_2); // doesn't show...
        }
    }
}

[App.xaml]

<Application
    x:Class="AppListViewPath.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AppListViewPath"
    RequestedTheme="Light">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Common/StandardStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>

            <Style x:Key="PathCustomStyle" TargetType="Path">
                <Setter Property="Data" Value="M95,15 C95,20 95,20 95,20"/>
                <Setter Property="Width" Value="200"/>
                <Setter Property="Height" Value="200"/>
                <Setter Property="Stroke" Value="Red"/>
                <Setter Property="Stretch" Value="Fill"/>
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
            </Style>

        </ResourceDictionary>
    </Application.Resources>
</Application>

[MainPage.xaml]

<Page
    x:Class="AppListViewPath.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AppListViewPath"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <ListView x:Name="listView_path"/>
    </Grid>
</Page>
Теги:
xaml
windows-runtime
winrt-xaml
microsoft-metro

1 ответ

1
Лучший ответ

Проблема в том, что вы не можете использовать одну и ту же геометрию несколько раз в визуальном дереве, и стиль разрешает ссылку Path.Data на геометрию, когда она читает ее, а не назначает строку цели и затем ее разрешает. Поскольку геометрия уже используется, второй путь заканчивается без данных.

Вместо того, чтобы устанавливать данные непосредственно в стиле, вы можете создать ресурс String для данных и установить его отдельно от стиля:

<ResourceDictionary>
    <Style x:Key="PathCustomStyle" TargetType="Path">
        <Setter Property="Width" Value="200"/>
        <Setter Property="Height" Value="200"/>
        <Setter Property="Stroke" Value="Red"/>
        <Setter Property="StrokeThickness" Value="10" />
        <Setter Property="Stretch" Value="Fill"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>

    <x:String x:Key="PathDataString">M95,15 C95,20 95,20 95,20</x:String>
<ResourceDictionary>

Чтобы установить в Xaml:

<Path Style="{StaticResource PathCustomStyle}" Data="{StaticResource PathDataString}" />

Чтобы установить код:

object o;
Application.Current.Resources.TryGetValue("PathCustomStyle", out o);

string stringData = (string)Application.Current.Resources["PathDataString"];

string xamlPath = "<Path xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>" +
"<Path.Data>" + stringData + "</Path.Data></Path>";

// Shows 10
for (int i = 0; i < 10; i++)
{
    Path path = XamlReader.Load(xamlPath) as Path;
    path.Style = (Style)o;

    this.listView_path.Items.Add(path);
}

Ещё вопросы

Сообщество Overcoder
Наверх
Меню