我有我的目標,我想添加到我的ListView
:WPF:添加多個對象到我的ListView - 只有第一個加入
Public class MyData
{
string name {get; set;}
}
這是我嘗試添加代碼隱藏我的對象:
ObservableCollection<MyData> collection = new ObservableCollection<MyData>();
MyData data1 = new MyData("c:\file.doc");
collection.Add(data1);
myListView.Items.Add(collection);
這工作正常,但如果我想補充另一個問題:
ObservableCollection<MyData> collection = new ObservableCollection<MyData>();
MyData data1 = new MyData("c:\file.doc");
collection.Add(data1);
MyData data2 = new MyData("c:\blabla.doc");
collection.Add(data2);
myListView.Items.Add(collection);
,我只能看到第一個對象。
ListView
XAML:
<ListView Name="myListView" Margin="16,453,263,40" Background="Transparent" BorderThickness="0" >
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Foreground" Value="White"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.Resources>
<DataTemplate x:Key="MyDataTemplate">
<Grid Margin="-6">
<ProgressBar Maximum="100" Value="{Binding Progress}"
Width="{Binding Path=Width, ElementName=ProgressCell}" Height="16" Margin="0"
Foreground="#FF5591E8" />
<Label Content="{Binding Progress, StringFormat={}{0}%}" FontSize="12"
VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>
</DataTemplate>
<ControlTemplate x:Key="ProgressBarTemplate">
<Label HorizontalAlignment="Center" VerticalAlignment="Center" />
</ControlTemplate>
</ListView.Resources>
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource ListViewHeaderStyle}">
<GridViewColumn Width="425" Header="File name" DisplayMemberBinding="{Binding FileName}" />
<GridViewColumn x:Name="ProgressCell" Width="50" Header="Progress"
CellTemplate="{StaticResource MyDataTemplate}" />
</GridView>
</ListView.View>
</ListView>
您可以發佈您的XAML。 – Contango
我會建議使用綁定來做到這一點。我強烈建議學習MVVM模式,它爲我創建可用的,可維護的技術債務免費應用程序提供了很好的幫助。 – Contango
查看我的更新請 –