WPF: Progress bar in ListView

2020-07-10 06:56发布

问题:

I'm trying to display information from my ObservableCollection<MyData> in a ListView. MyData has:

string Name
string Location
int Progress

Using DataBinding, I'm able to display the Name and Location for all the items in my ObservableCollection<MyData> in their own column. But how can I add a Progress column with a ProgressBar inside? Progress is a percentage.

回答1:

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


回答2:

Your ListView in XAML:

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

Code-behind:

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;


回答3:

Just bind Progress property in MyData to the ProgressBar.Value and set the expected MAX value as the ProgressBar.Maximum for example 50 below

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