ListView中的WPF进度条保持为空并添加新数据

时间:2015-05-31 13:51:16

标签: wpf listview data-binding progress-bar

我想要实现ProgressBar insine ListView

<ListView Name="lvTest" ItemsSource="{Binding PersonList}" Margin="308,45,268,473">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="390" Header="File name" DisplayMemberBinding="{Binding Name}"/>
            <GridViewColumn Width="50" Header="Progress">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <ProgressBar Maximum="100" Value="{Binding Progress}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

我的班级示例:

internal class MyData
{
    public string Name { get; set; }
    public int Progress { get; set; }
}

背后的代码:

var items = new ObservableCollection<MyData>();

items.Add(new MyData() { Name = "Some", Progress = 25 });
items.Add(new MyData() { Name = "Another", Progress = 91 });

lvTest.ItemsSource = items;

这就是结果:http://s12.postimg.org/3mgcwqbvh/image.png

正如您所见,我的进度条为空,我只能看到内部的点。

3 个答案:

答案 0 :(得分:2)

此处包含进度条中的文字。

    <ListView Name="lvTest">
        <ListView.Resources>
            <DataTemplate x:Key="MyDataTemplate">
                <Grid Margin="-6">
                    <ProgressBar Maximum="100" Value="{Binding Progress}" Width="{Binding Path=Width, ElementName=ProgressCell}" Height="10" Margin="0"/>
                    <TextBlock Text="{Binding Progress, StringFormat={}{0}%}" HorizontalAlignment="Center"/>
                </Grid>
            </DataTemplate>
        </ListView.Resources>
        <ListView.View>
            <GridView>
                <GridViewColumn Width="390" Header="File name" DisplayMemberBinding="{Binding Name}"/>
                <GridViewColumn x:Name="ProgressCell"  Width="50" Header="Progress" CellTemplate="{StaticResource MyDataTemplate}" />
            </GridView>
        </ListView.View>
    </ListView>

答案 1 :(得分:1)

问题是细胞的大小。您的ProgressBar是可见的 - 但您可以看到的只是小点。 将ProgressBar宽度绑定到单元格的宽度。

    <ListView Name="lvTest">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="390" Header="File name" DisplayMemberBinding="{Binding Name}"/>
                <GridViewColumn x:Name="ProgressCell"  Width="50" Header="Progress">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <ProgressBar Maximum="100" Value="{Binding Progress}" Width="{Binding Path=Width, ElementName=ProgressCell}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

答案 2 :(得分:-1)

向进度条添加width属性可以解决您的问题,即

<ProgressBar Maximum="100" Value="{Binding Path=Progress}" Width="100"/>