Hi guys,
Recently I'm working on a new WPF project. And i'm trying to implement the DataModel-View-ViewModel pattern which posted on Dan Crevier's blog. http://blogs.msdn.com/dancre/archive/2006/10/11/datamodel-view-viewmodel-pattern-series.aspx
And I'm come across a problem regarding command binding.
My code is something like this
<Grid><Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="30"/> </Grid.RowDefinitions> <igDP:XamDataGrid Grid.Row="0" x:Name="XamDataGrid1" DataSource="{Binding Files}"> <igDP:XamDataGrid.FieldLayoutSettings> <igDP:FieldLayoutSettings AutoGenerateFields="False" /> </igDP:XamDataGrid.FieldLayoutSettings> <igDP:XamDataGrid.FieldLayouts> <igDP:FieldLayout> <igDP:FieldLayout.Fields> <igDP:Field Name="File" Label="File" Visibility="Visible"/> <igDP:UnboundField Visibility="Visible"> <igDP:Field.Settings> <igDP:FieldSettings> <igDP:FieldSettings.CellValuePresenterStyle> <Style TargetType="{x:Type igDP:CellValuePresenter}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <Button x:Name="_btnFind" Content="Find" Command="{Binding FindCommandModel.Command}" DM:CreateCommandBinding.Command="{Binding FindCommandModel}" CommandParameter={Binding Path=DataItem}"/> </ControlTemplate> </Setter.Value> </Setter> </Style> </igDP:FieldSettings.CellValuePresenterStyle> </igDP:FieldSettings> </igDP:Field.Settings> </igDP:UnboundField> </igDP:FieldLayout.Fields> </igDP:FieldLayout> </igDP:XamDataGrid.FieldLayouts> </igDP:XamDataGrid> <Button Grid.Row="1" x:Name="_btnSubmit" Content="Submit" Command="{Binding SubmitCommandModel.Command}" DM:CreateCommandBinding.Command="{Binding SubmitCommandModel}"/> <Grid>
Basically, what i'm doing here is pretty straight forward.
I set the page DataContext to a ViewModel.
Inside the view model it contain properties
public BindingList<FileModel> Files public CommandModel FindCommandModel public CommandModel SubmitCommandModel
I was able to show the Files list on the grid. (Datasource binding is working)I was able to route the _btnSubmit event to my ViewModel (_btnSubmit command binding is fine)
But my problem is on the _btnFind, the command binding seem doesn't work.I aware that the _btnFind is nested inside the grid, and the grid datasource is Files.If I'm not mistaken, in WPF the binding will automatically search upward the hierarchy to look for the item.I'm out of idea how should I fix this.Hopefully, some expert in WPF may give me a help.Thanks in advance.
Hello philippe,
I believe the binding you need for the Button will have a RelativeSource where CommandParameter = {Binding RelativeSource={RelativeSource TemplatedParent}, Path=Record.DataItem}. Because the Style's TargetType is the CellValuePresenter, you have to bind to the button's TemplatedParent. From there, the path will be the property path from the CellValuePresenter, ie CVP.Record.DataItem.
Hope that helps.