2

Context: a C# 4.0 WPF application with a datagrid that has one TemplateColumn showing a progress bar.

How can one get the grid to only display the progress bar for certain items based on a condition?

Maybe listening to the events and hiding the cells / setting visibile to false would be an option.

This is how it looks right now (the progress bar is shown for all items):

<UserControl.Resources>
    <DataTemplate x:Key="PotentialDataTemplate">
        <Grid Width="70">
            <ProgressBar
                Height="12"
                VerticalAlignment="Center"
                Value="{Binding Path=Potential, Mode=OneWay}" />
        </Grid>
    </DataTemplate>
</UserControl.Resources>

<DataGrid x:Name="dataGrid"
        ItemsSource="{Binding Path=Items}"
        AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTemplateColumn
            Header="{Binding Source={x:Static text:TextBindingProvider.Instance}, Path=CompendiumHeaderPotential}"
            Width="Auto"
            MinWidth="80"
            CellTemplate="{StaticResource PotentialDataTemplate}"
            IsReadOnly="true"
            SortMemberPath="Potential" />
    </DataGrid.Columns>
</DataGrid>

Cilvic
  • 3,417
  • 2
  • 33
  • 57

2 Answers2

2

You have a couple of options depending on what the conditions are for visibility. If you have a separate property such as "IsPotentialVisible" then you can bind this to the Visibility property of the progressbar using a BooleanToVisibilityConverter.

Next up, if it is a simple condition such as "hide when Potential == 0", then you could create a DataTrigger that handles this condition.

Otherwise you can also create a custom converter that spits out a visibility based on whatever input properties / parameters are required.

Andrew Hanlon
  • 7,271
  • 4
  • 33
  • 53
  • Thanks for giving me those other options! For now I implemented my own property but I will look at DataTriggers too. What exactly do you mean with converter? is that a WPF concept I can look up? – Cilvic Apr 27 '11 at 15:25
  • 1
    Hey, bindings in WPF allow you to specify a converter which is a class derieved from the IValueConverter interface: http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx it allows for complex binding and lots of other useful conversions. – Andrew Hanlon Apr 27 '11 at 15:36
2

Just found an answer, I simply add the Visibility attribute and Bind it to some conditional Logic in the ViewModel.

    Visibility = "{Binding Path=ShowPotentialBar, Mode=OneWay}

So:

    <Grid Width="70">
        <ProgressBar
            Height="12"
            VerticalAlignment="Center"
            Value="{Binding Path=Potential, Mode=OneWay}" 

            Visibility = "{Binding Path=ShowPotentialBar, Mode=OneWay}" />

    </Grid>
Cilvic
  • 3,417
  • 2
  • 33
  • 57
  • Unless the ShowPotentialBar property is a Visibility enum type, this won't work without a BooleanToVisibilityConverter. – Andrew Hanlon Apr 27 '11 at 15:49
  • Actually it works with String return type, returning "Hidden", "Visible" or "Colapsed" – Cilvic Apr 27 '11 at 18:25
  • In that case the default binding converter is converting the string to it's enum equivalent - which I don't think many people would say is a good practice. – Andrew Hanlon Apr 28 '11 at 14:03
  • @ach Thank you, changed it to System.Windows.Visibility.Visible – Cilvic Apr 28 '11 at 15:05