【问题标题】:WPF Progress bar in ListView remained empty and add new dataListView 中的 WPF 进度条保持为空并添加新数据
【发布时间】:2015-05-31 13:51:15
【问题描述】:

我想在我的ListView 中实现ProgressBar

<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

你可以看到我的进度条是空的,我只能看到里面的点。

【问题讨论】:

  • 克里斯蒂安的回答是不对的!如果您的单元格的宽度为 50,而进度条的宽度为 100,您将看不到进度条的 50%!
  • @Verint Verint:一定要显示 % 查看我修改后的答案:在进度条中测试。

标签: wpf listview data-binding progress-bar


【解决方案1】:

这里有进度条中的文字。

    <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>

【讨论】:

  • 这工作正常,您可以按要求显示文本。
【解决方案2】:

问题是单元格的大小。你的 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>

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 修复宽度是不可能的解决方案,导致单元格宽度可能改变!
    • 非常感谢,顺便说一句,是否可以在此进度条中添加文本?​​
    猜你喜欢
    • 2013-07-29
    • 1970-01-01
    • 2021-04-26
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    相关资源
    最近更新 更多