簡體   English   中英

WPF:從DataTemplate中的Listview中的textblock設置ListViewItem

[英]WPF: Set ListViewItem from textblock in the Listview within a DataTemplate

我有一個帶有ListView的Windows Phone 8.1項目,其后面的c#代碼填充了它的itemssource。 它工作,但我最終在單行文本塊之間有空格。 我已經嘗試在文本塊上設置高度,它位於列表視圖內部。 我嘗試設置一個ItemContainerStyle,將高度綁定到文本塊的高度,但它不起作用。
如果我將TextBlock的文本設置為Actual Height綁定,我得到0,所以我一定做錯了。 我很確定它與ListViewItems的高度有關,但由於它們是從代碼填充的,我無法弄清楚如何讓它們做我想做的事情。 我也嘗試切換到列表的ItemsControl但它似乎沒有滾動和工作。 這是Listview的XAML:

<ListView x:Name="TheList" IsHoldingEnabled="True"
                      ItemsSource="{Binding items}"
                      Loaded="WhenListViewBaseLoaded"
                      ContinuumNavigationTransitionInfo.ExitElementContainer="True"
                      IsItemClickEnabled="True">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Height" Value="{Binding ElementName=txtBibleText, Path=ActualHeight}"/>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid x:Name="ItemTemplateGrid" Holding="ListViewItem_Holding" Background="Blue">
                        <FlyoutBase.AttachedFlyout>
                            <MenuFlyout>
                                <MenuFlyoutItem Text="Share"
                                                Click="ShareFlyoutItem_Click" />
                                <MenuFlyoutItem Text="Add to Sharing"
                                                Click="AddSharingFlyoutItem_Click" />
                            </MenuFlyout>
                        </FlyoutBase.AttachedFlyout>
                        <Grid x:Name="gridText">
                            <TextBlock x:Name="txtBibleText" 
                                   FontSize="{Binding TheFontSize}"
                                   Grid.Column="1"
                                   VerticalAlignment="Top"
                                   HorizontalAlignment="Left"
                                   TextWrapping="Wrap"
                                   Margin="0,0,0,0" FontFamily="Global User Interface">
                            <Run Text="{Binding VerseNumber}"/>
                            <Run Text="{Binding BibleText}"/>
                            </TextBlock>
                        </Grid>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

填充ListView的代碼背后:

XDocument loadedData = XDocument.Load(TranlationFilePath);
            var data = from query in loadedData.Descendants("testament").Descendants("book").Descendants("chapter").Descendants("verse")
                       where (string)query.Parent.Parent.Parent.Attribute("name") == GetTestament
                       where (string)query.Parent.Parent.Attribute("name") == GetBibleBook
                       where (string)query.Parent.Attribute("number") == GetChapter
                       select new BibleLoad
                       {
                           VerseNumber = (string)query.Attribute("number"),
                           BibleText = (string)query.Value.ToString(),
                           TheFontSize = FontSize
                       };
            TheList.ItemsSource = data;

感謝您的時間。 這是我第一次發帖提問,希望我做得對。 我已經搜索,搜索和實驗了很長一段時間。

在此輸入圖像描述

編輯XML並縮短記錄后。 隨着第8節編輯

關閉文本包裝。

在此輸入圖像描述

重新打開時,高度設置為20,最小高度設置為31。 在此輸入圖像描述

MinHeight到20包裝:

在此輸入圖像描述

額外的空間來自ItemContainerStyle的ListViewItem模板。 默認模板不僅包括ItemTemplate的空間,還包括用於標記選擇的復選框等裝飾。 注意CheckboxContainers的Rectangle的高度為25.5,SelectedCheckMark的高度為34。

<Grid x:Name="CheckboxContainer">
    <Grid.RenderTransform>
        <TranslateTransform x:Name="CheckboxContainerTranslateTransform" X="{ThemeResource ListViewItemContentOffsetX}"/>
    </Grid.RenderTransform>
    <Rectangle x:Name="NormalRectangle" Fill="{ThemeResource CheckBoxBackgroundThemeBrush}" Height="25.5" Stroke="{ThemeResource CheckBoxBorderThemeBrush}" StrokeThickness="{ThemeResource CheckBoxBorderThemeThickness}" Width="25.5"/>
    <Path x:Name="CheckGlyph" Data="M0,123 L39,93 L124,164 L256,18 L295,49 L124,240 z" Fill="{ThemeResource CheckBoxForegroundThemeBrush}" FlowDirection="LeftToRight" HorizontalAlignment="Center" Height="17" IsHitTestVisible="False" Opacity="0" Stretch="Fill" StrokeThickness="2.5" StrokeLineJoin="Round" VerticalAlignment="Center" Width="18.5"/>
</Grid>

<Border x:Name="SelectedBorder" BorderBrush="{ThemeResource ListViewItemSelectedBackgroundThemeBrush}" BorderThickness="{ThemeResource GridViewItemMultiselectBorderThickness}" IsHitTestVisible="False" Opacity="0">
    <Grid x:Name="SelectedCheckMark" HorizontalAlignment="Right" Height="34" Opacity="0" VerticalAlignment="Top" Width="34">
        <Path x:Name="SelectedEarmark" Data="M0,0 L40,0 L40,40 z" Fill="{ThemeResource ListViewItemSelectedBackgroundThemeBrush}" Stretch="Fill"/>
        <Path x:Name="SelectedGlyph" Data="M0,123 L39,93 L124,164 L256,18 L295,49 L124,240 z" Fill="{ThemeResource ListViewItemCheckThemeBrush}" FlowDirection="LeftToRight" HorizontalAlignment="Right" Height="14.5" Margin="0,1,1,0" Stretch="Fill" VerticalAlignment="Top" Width="17"/>
    </Grid>
</Border>

如果您不需要選擇行為,則可以將ItemContainerStyle剝離為您需要的部分,這樣就不必為與您的應用程序無關的裝飾騰出空間。 如果您確實需要選擇,您可以移動或調整選擇檢查的大小,以便它們適合您的設計。

您可以通過在設計器中選擇ListView來生成默認的ItemContainerStyle模板,右鍵單擊並選擇Edit Additional Templates.Edit Generated Item Container(ItemContainerStyle)編輯副本... 在此輸入圖像描述

然后,您可以根據需要編輯裝飾高度。

你為什么要設置minheight或height? 嘗試給出一個字體大小,它會調整高度本身保持textwrapping = wrap ...另外一件事你為什么設置grid.colum = 1? ..你只有一列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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