views:

72

answers:

1

In this sample I have two buttons. The button in the header works, but the one in the grid gives me the error show below.

<GroupBox DockPanel.Dock="Top" >
    <GroupBox.Header>
        <StackPanel Orientation="Horizontal">
            <Label Content="Recent Servers" />
            <CheckBox Content="Auto-Refresh" />
            <Button Content="Monitor" Command="{Binding MonitorCommand, ElementName=Top}"  CommandParameter="{Binding}" />
        </StackPanel>
    </GroupBox.Header>
    <DataGrid CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False" ItemsSource="{Binding ServerHistory}" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="Machine Name" Binding="{Binding DataPoints[ServerName], Mode=OneWay}" />
            <DataGridTextColumn Header="Last Heartbeat" Binding="{Binding DataPoints[LastHeartbeat], Mode=OneWay}"/>
            <DataGridTemplateColumn Header="Monitor">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Monitor" Command="{Binding MonitorCommand, ElementName=Top}"  CommandParameter="{Binding}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</GroupBox>

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=Top'. BindingExpression:Path=MonitorCommand; DataItem=null; target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand')

A: 

I believe it's because your second button is enclosed as part of a template. Have a click event instead, grab it in the code behind and try to work out the sender's relationship to your data, then issue the command trigger.

keyle
I found the answer: http://stackoverflow.com/questions/581715/binding-to-a-command-in-a-datagrid/581778#581778
Jonathan Allen
Ah, that seems to be a better solution.
keyle