UniformGrid 的 XAML 项模板弄乱了边距和位置

XAML Item Template for UniformGrid messed up margins and positions

提问人:Moni 提问时间:10/28/2023 更新时间:10/28/2023 访问量:50

问:

我试图制作一个 UniformGrid,其中包含作为子项出售的物品,但在添加它们后,边距和位置变得混乱。

它应该看起来像这样enter image description here

但它看起来像这样enter image description here

这是代码! 项模板:

<UserControl x:Class="ShopWave.MVVM.ItemListingTemplate"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:ShopWave.MVVM"
         mc:Ignorable="d" 
         d:DesignHeight="500" d:DesignWidth="400">
<Border Margin="10"
        Background="#29303d"
        CornerRadius="5">


    <Grid Margin="0">

        <Image Source="\Images\placeholderImage.jpg"
               Width="300"
               Height="200"
               VerticalAlignment="Top"
               Margin="0,10,0,0"
               x:Name="img"/>

        <TextBlock Text="Name"
                   Foreground="White"
                   Width="290"
                   Height="40"
                   FontSize="25"
                   VerticalAlignment="Top"
                   HorizontalAlignment="Center"
                   Margin="-8,170,0,0"
                   x:Name="name"/>

        <TextBlock Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labo."
                   TextWrapping="WrapWithOverflow"
                   Foreground="LightGray"
                   TextAlignment="Left"
                   Height="200"
                   Width="285"
                   FontSize="20"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   Margin="-8,120,0,0"
                   x:Name="dscp"/>

        <TextBlock Text="$16.99"
                   Foreground="DarkGray"
                   Width="90"
                   Height="30"
                   FontSize="25"
                   TextAlignment="Center"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Center"
                   Margin="0,400,0,0"
                   x:Name="price"/>

        <Button Background="Transparent"/>
    </Grid>
</Border>

和统一网格的代码:

<UserControl x:Class="ShopWave.MVVM.View.ServiceView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:ShopWave.MVVM.View"
         mc:Ignorable="d" 
         Loaded="ServiceView_Loaded"
         d:DesignHeight="450" d:DesignWidth="800">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="11*"/>
        <RowDefinition Height="139*"/>
    </Grid.RowDefinitions>

    <TextBlock Text="Services" 
               Foreground="White"
               VerticalAlignment="Center"
               HorizontalAlignment="Left"
               Margin="20,0,0,0"
               FontSize="18"
               Grid.Row="0"/>

    <ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1">
        <UniformGrid x:Name="serviceStackPanel"
                    Margin="10"
                    VerticalAlignment="Top">

        </UniformGrid>
    </ScrollViewer>
</Grid>

知道为什么会发生这种情况吗? 任何帮助都是值得赞赏的!

C# .NET WPF 可视化工作室 XAML

评论

1赞 Joe 10/28/2023
你首先如何获得不止一件物品?是否有某种我没有看到的 ItemsControl?
0赞 Moni 10/28/2023
我使用 foreach 循环来做到这一点
0赞 Clemens 10/28/2023
虽然使用了正确的术语“项模板”,但未能使用相应的 WPF 控件。应使用 ItemsControl,它使用 UniformGrid 作为其 ItemsPanel,并使用 DataTemplate,并将 ItemListingTemplate 作为其 ItemTemplate。请参阅数据模板化概述
0赞 Moni 10/28/2023
好吧,但你这是什么意思?我真的无法想象你在暗示什么。
0赞 Joe 10/28/2023
若要在 WPF 面板上显示一堆项,请使用绑定到所选项的 f。有一个属性,您可以设置该属性,该属性包含一个 .这只会绘制一个项目。因此,您放下 ,定义它,将其绑定到项列表,然后从集合中添加/删除。它负责为您绘制它们。这可能会有所帮助:stackoverflow.com/questions/42372388/......ItemsControlObservableCollectionItemsControlItemTemplateDataTemplateDataTemplateItemsControlItemTemplate

答:

-1赞 Ali Jafari 10/28/2023 #1

你可以这段代码

    <UserControl x:Class="ShopWave.MVVM.ItemListingTemplate"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:ShopWave.MVVM"
     mc:Ignorable="d" 
     d:DesignHeight="500" d:DesignWidth="400">
      <Button Margin="10"
    Background="#29303d"
    CornerRadius="5">
    <Button.Content>
<DockPanel>

    <Image Source="\Images\placeholderImage.jpg"
           Width="300"
           Height="200"
           VerticalAlignment="Top"
           Margin="0,10,0,0"
           x:Name="img"
           DockPanel.Dock="Top"/>

    <TextBlock Text="Name"
               Foreground="White"
               Width="290"
               Height="40"
               FontSize="25"
               VerticalAlignment="Top"
               HorizontalAlignment="Center"
               Margin="-8,170,0,0"
               x:Name="name"
               DockPanel.Dock="Top"/>

    <TextBlock Text="$16.99"
               Foreground="DarkGray"
               Width="90"
               Height="30"
               FontSize="25"
               TextAlignment="Center"
               VerticalAlignment="Center"
               HorizontalAlignment="Center"
               Margin="0,400,0,0"
               x:Name="price"
               DockPanel.Dock="Bottom"/>

<TextBlock Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
               Sed do eiusmod tempor incididunt ut labo."
               TextWrapping="WrapWithOverflow"
               Foreground="LightGray"
               TextAlignment="Left"
               Height="200"
               Width="285"
               FontSize="20"
               HorizontalAlignment="Center"
               VerticalAlignment="Center"
               Margin="-8,120,0,0"
               x:Name="dscp"/>
    </DockPanel>
</Button.Content>
</Button>