mortenstarck
mortenstarck

Reputation: 2803

Progressbar in DataGrid

Im trying to add an progress bar to a column like so

<DataGrid Grid.Row="1" ItemsSource="{Binding Locations}" AutoGenerateColumns="false">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Plant}" Header="Plant" />
        <DataGridTextColumn Binding="{Binding Area}" Header="Area" />
        <DataGridTextColumn Binding="{Binding LocationName}" Header="Location" />
        <DataGridTextColumn Binding="{Binding LocationVersion}" Header="Version on location" />

        <DataGridCheckBoxColumn Binding="{Binding Deploy, Mode=TwoWay}" Header="Check to deploy" IsReadOnly="False" />
        <DataGridTemplateColumn Header="Orders Count" Width="380">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Grid>
                        <ProgressBar Grid.Row="0" Grid.Column="0"  Minimum="0" Maximum="200" Value="{Binding copyToLocationProgress ,Mode=OneWay}"/>
                    </Grid>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

This is the ILocation which i'm using:

 public interface ILocation
    {
        int LocationId { get; }

        string LocationName { get; set; }

        string Area { get; set; }

        string LocationVersion { get; set; }

        int Plant { get; set; }

        bool Deploy { get; set; }

        string FolderName { get; }

        int copyToLocationProgress { get; set; }
    }

This is in the ViewModel

public IEnumerable<ILocation> Locations { get; set; }

I have also tried it this way

 <DataGridTemplateColumn Header="Deploy progress" Width="380">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <Grid>
                                                <ProgressBar
                                                    Grid.Row="0"
                                                    Grid.Column="0" 
                                                    Minimum="0"
                                                    Maximum="200" 
                                                    Value="{Binding Path=DataContext.Locations, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"/>
                                            </Grid>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>

But this gave me this error:

System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='' BindingExpression:Path=DataContext.Locations; DataItem='DataGrid' (Name=''); target element is 'ProgressBar' (Name=''); target property is 'Value' (type 'Double')

But I can't see the value like on the other columns. Because it's databind to a Grid.

Upvotes: 2

Views: 4659

Answers (2)

nikhil
nikhil

Reputation: 1736

I am late but use something like this. In the template column you need to use cell template and then use the item container template.

 <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                        <ItemContainerTemplate>

                            <Grid>
                                <ProgressBar></ProgressBar>
                            </Grid>
                        </ItemContainerTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

Upvotes: 1

2432683
2432683

Reputation: 108

TemplateBinding can only be used within a ControlTemplate, you're using it within a DataTemplate. (The fact that the DataTemplate is within a ControlTemplate doesn't matter).

Change your xaml:

   <ItemContainerTemplate >
      <Grid>
         <ProgressBar Grid.Row="0" Grid.Column="0"  Minimum="0" Maximum="200" Value="{Binding CopyToLocationProgress ,Mode=OneWay}"/>
      </Grid>
   </ItemContainerTemplate>

and everything should be ok.

Upvotes: 1

Related Questions