2

In DataGrid I have a column defined like this.

<DataGridTemplateColumn Width="Auto">
    <DataGridTemplateColumn>
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                APPROACH
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

First approach

<Button Template="{StaticResource CloseTabButton}" Command="{Binding DataContext.DeleteRangeCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" CommandParameter="{Binding}"/>

Second approach

<Button Template="{StaticResource CloseTabButton}" Command="{Binding ElementName=CurrentWindow, Path=DataContext.DeleteRangeCommand}" CommandParameter="{Binding}"/>

In ViewModel:

public RelayCommand<Range> DeleteRangeCommand
{
    get { return _deleteRangeCommand ?? (_deleteRangeCommand = new RelayCommand<Range>((x) => ReportGroup.Ranges.Remove(x))); }
}

Both approaches should be identical. Problem is that, on some computers - I guess these without Visual Studio Installed, second approach doesn't work. Nothing is firing, I tried to attach with Remote Debugger and Command is not fired. Why is this possible?

OfCourse CurrentWindow is x:Name of Window with DataContext set to ViewModel

Tzah Mama
  • 1,547
  • 1
  • 13
  • 25
MistyK
  • 6,055
  • 2
  • 42
  • 76
  • I wouldnt say this is because visual studio is not installed. Sometimes binding skips a beat. In some special situations it doesnt get init properly at right time. In your situation the DataGridTemplateColumn is not part of logical or visual tree (it doesnt have datacontext). Therefore the Binding is doing nothing until CellTemplate for example gets applied to the cell control then the Binding becomes part of the visual tree and then it should start looking up. For binding to look up a fully funcional logical or visual tree is always needed else it fails. – dev hedgehog Jul 08 '14 at 13:59
  • 1) DataTemplate aren't part of visual tree or just CellTemplate? 2) Why sometimes it is working ? I understand that first approach may not work, but why it doesn't work on some computers and some computers supports it? – MistyK Jul 08 '14 at 14:30
  • 1
    Its hard to say why is it not working or sometimes working. There are many scenarions I can think of right now that might occur. One of them is as you might know Dispatchr works async and you might be still in loading process or in measuring process when dispatcher decided to push value to target or init binding. To fix this try {Binding Source={StaticResource blah}, Path="..." - this is how you will make sure that source is always there for binding. whenever the binding aks for getter and setter of the source it will be there. – dev hedgehog Jul 09 '14 at 07:25

0 Answers0