I have a XamDataGrid with a hyperlink cell. I'm trying to bind the Command on the Hyperlink cell to the command in the ViewModel, but having difficulty doing that.
Below is the XAML for the XamDataGrid
<ig:XamDataGrid DataSource="{Binding Report}" Theme="Office2k7Black" Height="600" Width="auto" ActiveDataItem="{Binding ActiveException, Mode=TwoWay}"> <ig:XamDataGrid.Resources> <Style TargetType="{x:Type ig:CellValuePresenter}" x:Key="hyperlinkCell"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ig:CellValuePresenter}"> <TextBlock Margin="{TemplateBinding Padding}"> <Hyperlink NavigateUri="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value}" Command="{Binding LoadDetailReport}"> <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value}"> </TextBlock> </Hyperlink> </TextBlock> </ControlTemplate> </Setter.Value> </Setter> </Style> </ig:XamDataGrid.Resources> <ig:XamDataGrid.FieldLayoutSettings> <ig:FieldLayoutSettings AutoGenerateFields="False"/> </ig:XamDataGrid.FieldLayoutSettings> <ig:XamDataGrid.FieldSettings> <ig:FieldSettings AllowFixing="NearOrFar" AllowEdit="False" Width="auto" Height="auto" /> </ig:XamDataGrid.FieldSettings> <ig:XamDataGrid.FieldLayouts> <ig:FieldLayout> <ig:FieldLayout.Fields> <ig:Field Name="Division" Label="Division"> <ig:Field.Settings> <ig:FieldSettings AllowResize="False" /> </ig:Field.Settings> </ig:Field> <ig:Field Name="Cluster" Label="Cluster" /> <ig:Field Name="Productline" Label="Product Line" /> <ig:Field Name="Mainbook" Label="Risk Group" Visibility="Collapsed"/> <ig:Field Name="Subbook" Label="Sub Book" Visibility="Collapsed"/> <ig:Field Name="Cobdate" Label="Cob Date"/> <ig:Field Name="TotalTrades" Label="Total Trades"> <ig:Field.Settings> <ig:FieldSettings CellValuePresenterStyle="{StaticResource hyperlinkCell}"/> </ig:Field.Settings> </ig:Field> <ig:Field Name="TotalExceptions" Label="Total Exceptions"/> <ig:Field Name="LiveTradesWithoutPVs" Label="Live Trades Without PVs"/> <ig:Field Name="ZeroPVCount" Label="Zero PV Count"> <ig:Field.Settings> <ig:FieldSettings CellValuePresenterStyle="{StaticResource hyperlinkCell}"/> </ig:Field.Settings> </ig:Field> <ig:Field Name="StalePVCount" Label="Stale PV Count"/> <ig:Field Name="RegressedPVCount" Label="Regressed PV Count"/> <ig:Field Name="TotalExceptionsValidated" Label="Total Exceptions Validated"/> <ig:Field Name="TotalExceptionsNotValidated" Label="Total Exceptions Not Validated"/> </ig:FieldLayout.Fields> </ig:FieldLayout> </ig:XamDataGrid.FieldLayouts> </ig:XamDataGrid>
This is the error I see in the output window
System.Windows.Data Error: 39 : BindingExpression path error: 'LoadDetailReport' property not found on 'object' ''DataRecord' (HashCode=15172893)'. BindingExpression:Path=LoadDetailReport; DataItem='DataRecord' (HashCode=15172893); target element is 'Hyperlink' (HashCode=57769674); target property is 'Command' (type 'ICommand')
From the error, I'm assuming that the parent DataContext which is the ViewModel is not visible to the Cell in which I have the hyperlink,
Can anybody give me some direction on this. Any help very much appreciated.
Thanks,
Shravan
Hello Shravan,
The DataContext of the CellValuePresenter is the DataRecord and that is what this exceptions says. If LoadDetailReport is a command on your data item, then you can change the expression to DataItem.LoadDetailReport. If this is on your main view model, you would have to explicitly target the view model. You can do that by getting to the DataContext of the XamDataGrid (through the DataPresenter property of the CVP) or using a RelativeSource - FindAncestor mode and routing upwards to the correct DataContext.
Hi Alex
I am also facing some issue related to command
I have an image in cellvaluePresenter now i want to open a new window on that image click and want to pass some paramenter on that new opened window on image click
My code is here
<igDP:XamDataGrid.Resources> <Style TargetType="{x:Type igDP:CellValuePresenter}" x:Key="EditButton"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"> <Button x:Name="btnEdit" Background="White" Height="25" Canvas.Top="5" Canvas.Left="25" Command="{Binding DataItem.DiDetailViewCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" CommandParameter="{Binding Path=ID}" > <Grid> <Image Width="20" Height="15" Source="/Images/Edit.png" x:Name="imgEdit" /> </Grid> </Button> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </igDP:XamDataGrid.Resources>
<igDP:XamDataGrid.FieldLayouts> <igDP:FieldLayout> <igDP:FieldLayout.Fields> <igDP:UnboundField Width="25"> <igDP:UnboundField.Settings> <igDP:FieldSettings CellValuePresenterStyle="{StaticResource ResourceKey=EditButton}" /> </igDP:UnboundField.Settings> </igDP:UnboundField> <igDP:Field Name="ID" Visibility="Collapsed" Width="*" > </igDP:Field>
</igDP:FieldLayout.Fields> </igDP:FieldLayout> </igDP:XamDataGrid.FieldLayouts>
in code i am doing like this
public ICommand DiDetailViewCommand { get { return _detailViewCommand ?? (_detailViewCommand = new RelayCommand(OpenDiDetailviewr , param => true )); } }
private void OpenDiDetailviewr(object param) {
ClsProperties.sTmpIntValue = Convert.ToInt32(param);
}
here i am unable to get value of paran , param value is null , please suggest what i am missing in my code
It has been done by passing DataItem on passing value
<Button x:Name="btnEdit" Background="White" Height="25" Canvas.Top="5" Canvas.Left="25" Command="{Binding Path=DataContext.DiDetailViewCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" CommandParameter="{Binding Path=DataItem.ID, Mode=TwoWay}"> <Grid> <Image Width="20" Height="15" Source="/Images/Edit.png" x:Name="imgEdit" /> </Grid> </Button>