[英]WPF ItemTemplate Causing ListBox Content Size Issue
在 WPF 項目中,如果我在 XAML 中手動插入項目,我有一個可以正確呈現的ListBox
,例如:
<ListBoxItem>
<Grid Background="#7F271043" Width="200" Height="200">
<Grid.RowDefinitions>
<RowDefinition Height="110" />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Image Grid.Row="0" Source="Logo-40p.png" HorizontalAlignment="Center" Margin="10,10,10,10" />
<TextBlock Grid.Row="1" Text="Test Item" Foreground="White" Margin="10,0,10,0" FontSize="16" VerticalAlignment="Center" />
<local:Rating Grid.Row="2" HorizontalAlignment="Left" Height="24" Margin="10,0,0,0" VerticalAlignment="Center" Width="130" SelectedValue="4" IsReadOnly="True" />
<TextBlock Grid.Row="2" Text="Free" Foreground="#FF969292" Margin="0,0,10,0" FontSize="14" HorizontalAlignment="Right" VerticalAlignment="Center" />
</Grid>
</ListBoxItem>
ListBoxItem
的底部如下所示:
但是,當我將<ListBoxItem>
換成:
<ListBox.ItemTemplate>
<DataTemplate>
即使我保持其他所有內容相同,它也會在我的評級控件中產生一個我無法修復的尺寸問題(不響應手動尺寸或拉伸設置):
關於如何解決這個問題的任何想法? ItemTemplate
/ DataTemplate
是否會導致控件不遵守手動設置的大小或由其所在的網格行設置的大小?
設法解決了這個問題 - 似乎 XAML 中手動定義的ListBoxItem
的行為與ItemTemplate
/ DataTemplate
的 output 不同。
我的評級用戶控件具有指定的設置高度和寬度,該高度和寬度被ListBoxItem
內的實現 XAML(高度/寬度/拉伸)覆蓋,但不在ItemTemplate
/ DataTemplate
內:
mc:Ignorable="d" Height="750" Width="4070" MouseLeave="UserControl_MouseLeave">
所以我改為將Height
/ Width
更改為DesignHeight
/ DesignWidth
來解決這個問題:
mc:Ignorable="d" d:DesignHeight="750" d:DesignWidth="4070" MouseLeave="UserControl_MouseLeave">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.