[英]Progressbar in listboxitem
我有一個包含項目的列表框,我使用datatemplate顯示項目
我要創建一個下載按鈕,一旦用戶選擇一個項目並單擊該按鈕,進度條就會添加到所選項目數據模板中,並顯示下載進度,這可能嗎?
任何指針都會有所幫助
<ListBox Name="List" Grid.Row="2"
ScrollViewer.HorizontalScrollBarVisibility="Visible"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
ItemTemplate="{StaticResource ListTemplate}"
ItemsSource="{Binding Path=Items}"
SelectedItem="{Binding Path=SelectedItem}"
ItemContainerStyle="{StaticResource RoundedItem}"
BorderBrush="{x:Null}" BorderThickness="0"
Background="{x:Null}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Margin="10" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
謝謝
對的,這是可能的。 您可以創建<ItemTemplate>
和<ItemTemplate>
,其中包含項目的“常規”表示形式以及進度條。 最初,您將進度條的Visibility
設置為Collapsed
。 下載開始時,將“ Visibility
更改為“ Visible
(您可能還希望折疊該項目的其他部分)。
您可以將Visibility
屬性綁定到該項目的視圖模型的屬性。 您可以使用IsDownloading
類的布爾屬性,然后通過將true
轉換為Visible
並將false
轉換為Collapsed
的轉換器進行綁定。
要顯示進度條,您只需將IsDownloading
設置為true並觸發PropertyChanged
事件。 下載完成后,將IsDownloading
設置為false並再次觸發PropertyChanged
事件。
這是有關如何實現的粗略草圖。 您需要為ListView
中的項目提供一個視圖模型類,您可以將其數據綁定到:
class Item : INotifyPropertyChanged {
public String Name { ... }
public Double Progress { ... }
public Boolean IsDownloading { ... }
...
}
您還需要一個IValueConverter
,它可以將Boolean
轉換為Visibility
:
class VisibilityConverter : IValueConverter {
public Boolean Invert { get; set; }
Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture) {
var visible = Convert.ToBoolean(value) ^ Invert;
return visible ? Visibility.Visible : Visibility.Collapsed;
}
Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture) {
throw NotImplementedException();
}
}
您的視圖應基於以下內容:
<ListBox
<ListBox.Resources>
<local:VisibilityConverter x:Key="VisibilityConverter"/>
<local:VisibilityConverter x:Key="InverseVisibilityConverter" Invert="True"/>
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock
Text="{Binding Name}"
Visibility="{Binding IsDownloading, Converter={StaticResource InverseVisibilityConverter}}"/>
<ProgressBar
Value="{Binding Progress}"
Visibility="{Binding IsDownloading, Converter={StaticResource VisbilityConverter}}"/>
</Grid>
<DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.