1
我有DataTemplate
爲ListView
項目。在DataTemplate
的內部,我有一個Grid
控件,我需要的是ListView
的實際寬度,但沒有ScrollBar的寬度,因爲這樣一些內容會在滾動條的後面顯示。我無法使用ListViewItem
的寬度,因爲我正在使用此模板進行定義。我在這裏看到了WPF與祖先的一些答案,但我認爲這在WinRT中是不可能的。如何獲得沒有滾動條的ListView的ActualWidth
編輯: 我試着得到listview的scrollviewer ViewportWidth。這裏是我得到的代碼
<DataTemplate x:Key="ListViewTemplate">
<Grid Width="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ContainerViewportWidthConverter}}">
<Grid.RowDefinitions>...
這是我的listview使用的DataTemplate,它在一個單獨的xaml文件中。
轉換器看起來是這樣的:
if (!(value is FrameworkElement)) return null;
var ctrl = value as FrameworkElement;
var scrollViewer = ctrl.GetFirstAncestorOfType<ScrollViewer>();
return scrollViewer.ViewportWidth;
的ScrollViewer中變得總是null
和應用程序崩潰 當我試圖讓所有的祖先或後代,返回列表也是空的。
我試過了,但我是一個初學者,所以我不知道如何獲得listview ScrollViewer的參考,我不知道如何在XAML中綁定它。 –
簡單的答案是使用WinRT XAML工具包,但您可以通過VisualTreeHelper從控制模板中找到更多方法來查找ScrollViewer。檢查[這個問題]例如(http://stackoverflow.com/questions/14408408/how-do-you-programatically-scroll-to-the-bottom-of-a-textbox-in-winrt)。 –
我試過你的方法,我不能讓它工作。我編輯我的問題更具體。如果轉換器不是一個好主意,也許你可以給我建議把這些代碼放在哪裏。 –