簡體   English   中英

WPF ItemTemplate 導致列表框內容大小問題

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM