2

I have a DataGrid with three columns.

There is a delete button in the third column. Its visibility depends on boolean property called 'ControlsEnabled'.

DataGrid is populated by items in 'ObservableCollection'.

The problem is that 'ControlsEnabled' is not a property of items in our 'ObservableCollection' and it should not be. 'ObservableCollection' is property of another class.

Question: how should I change my XAML description to solve my problem?

<DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding Path=field1}" 
                        Width="140" 
                        Header="head1"/>
    <DataGridTextColumn Binding="{Binding Path=field2}" 
                        Width="140" 
                        MinWidth="50" 
                        Header="head2"/>

    <DataGridTemplateColumn>
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Padding="5" 
                        Click="DeleteButton_Click" 
                        Tag="{Binding}" 
                        Content="X" 
                        Visibility="{Binding Path=ControlsEnabled, Converter={StaticResource boolToVisibilityConverter}}">
                    <Button.ToolTip>
                        <TextBlock>
                            Delete
                        </TextBlock>
                    </Button.ToolTip>
                </Button>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>

</DataGrid.Columns>

Thank you!

iabbott
  • 873
  • 1
  • 8
  • 23
Jacek Wojcik
  • 1,223
  • 2
  • 19
  • 33

2 Answers2

5

You can use Binding RelativeSource. Read How do I use WPF bindings with RelativeSource?

For example:

{Binding Path=PathToProperty, RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}

Or use Binding ElementName to jump to specific control data context.

<Window ... x:Name="_this">
    <Button Visibility="{Binding ElementName=_this, Path=DataContext.ControlsEnabled, Converter={StaticResource boolToVisibilityConverter}}" />
Community
  • 1
  • 1
mswietlicki
  • 1,413
  • 12
  • 16
  • So if I have object called `xyz.ControlsEnabled` and I want to use it in my example then what should I do? `xyz` is not related to items in my `ObservableCollection` – Jacek Wojcik Sep 05 '13 at 13:54
  • @Jacek Depends what your `DataContext` is, but something like `xyz.ControlsEnabled` – jamesSampica Sep 05 '13 at 13:58
  • ControlsEnabled must be accessible though some control DataContext. e.g. if your main ViewModel have property ControlsEnabled and is bind to DataContext of your Window you will need to use my second example. – mswietlicki Sep 05 '13 at 14:04
0

ControlsEnabled should be a property on the item that is added to your ObservableCollection. That is if you are trying to delete that item from your collection..

denis morozov
  • 6,236
  • 3
  • 30
  • 45
  • The thing is that I do not want to iterate through all items in `ObservableCollection` and make them all `ControlsEnabled = true` or `ControlsEnabled = false`... – Jacek Wojcik Sep 05 '13 at 13:41