提问人:Moni 提问时间:10/28/2023 更新时间:10/28/2023 访问量:50
UniformGrid 的 XAML 项模板弄乱了边距和位置
XAML Item Template for UniformGrid messed up margins and positions
问:
我试图制作一个 UniformGrid,其中包含作为子项出售的物品,但在添加它们后,边距和位置变得混乱。
这是代码! 项模板:
<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>
知道为什么会发生这种情况吗? 任何帮助都是值得赞赏的!
答:
-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>
评论
ItemsControl
ObservableCollection
ItemsControl
ItemTemplate
DataTemplate
DataTemplate
ItemsControl
ItemTemplate