WPF:ListView中的进度条

时间:2009-11-10 04:09:15

标签: wpf data-binding listview progress-bar

我正在尝试在ObservableCollection<MyData>中显示ListView中的信息。 MyData有:

string Name
string Location
int Progress

使用DataBinding,我可以在自己的专栏中显示Name中所有项目的LocationObservableCollection<MyData>。但是,如何在Progress列中添加ProgressBarProgress是一个百分比。

3 个答案:

答案 0 :(得分:10)

<ListView ItemsSource="{Binding PersonList}">  
    <ListView.View> 
        <GridView>  
            <GridViewColumn Width="140" Header="GameName" DisplayMemberBinding="{Binding Name}"/>
            <GridViewColumn Width="140" Header="Progress">
                <GridViewColumn.CellTemplate>  
                    <DataTemplate>
                        <ProgressBar Maximum="100" Value="{Binding Progress}"/>
                    </DataTemplate>
                 </GridViewColumn.CellTemplate>  
            </GridViewColumn>
        </GridView>  
    </ListView.View>  
</ListView>

答案 1 :(得分:5)

您在XAML中的ListView:

<ListView x:Name="DataView">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <Label Content="{Binding Path=Name}" />
                    <ProgressBar Height="20" Width="100" Value="{Binding Path=Progress}" />
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
</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 });

DataView.ItemsSource = items;

答案 2 :(得分:1)

只需将MyData中的Progress属性绑定到ProgressBar.Value,并将预期的MAX值设置为ProgressBar.Maximum,例如下面的示例50

<ProgressBar Maximum="50" Value="{Binding Progress}" ..