簡體   English   中英

如何在沒有ListView滾動條的情況下獲取ActualWidth

[英]How to get the ActualWidth without scrollbar of a ListView

我有一個用於ListView項目的DataTemplate 在此DataTemplate內部,我有一個Grid控件,我需要該ListView的實際寬度,但沒有ScrollBar的寬度,因為這樣某些內容就會移到滾動條的后面。 我不能使用ListViewItem的寬度,因為我是用此模板定義它的。 我在這里看到了與祖先一起使用WPF的一些答案,但是我認為這在WinRT中是不可能的。

編輯:我試圖獲取listview的scrollviewer ViewportWidth。 這是我得到的代碼

<DataTemplate x:Key="ListViewTemplate">
      <Grid Width="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ContainerViewportWidthConverter}}">
            <Grid.RowDefinitions>...

這是我的列表視圖使用的DataTemplate,它位於單獨的xaml文件中。

轉換器看起來像這樣:

        if (!(value is FrameworkElement)) return null;
        var ctrl = value as FrameworkElement;
        var scrollViewer = ctrl.GetFirstAncestorOfType<ScrollViewer>();
        return scrollViewer.ViewportWidth;

當我嘗試獲取所有祖先或后代時,scrollViewer始終為null ,並且應用程序崩潰,返回列表也為空。

您是否嘗試過ScrollViewer.ViewportWidth

暫無
暫無

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

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