i have this in my view model:
DelegateCommand _test;
public ICommand TestCommand
{
get
if (_test == null)
_test = new Commands.DelegateCommand(
() => this.Test(),
() => this.CanTest);
}
return _test;
public void Test()
public bool CanTest { get { return true; } }
in my View... i have:
<igDP:XamDataPresenter.Resources>
<ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type igDP:CellValuePresenter}">
<Grid Width="16" Height="16">
<Rectangle Visibility="Collapsed" Fill="#FFBBBBBB" HorizontalAlignment="Left" Margin="0,1,0,0" x:Name="LeftBorder" VerticalAlignment="Center" Width="1"/>
<Border Margin="0,0,1,0" x:Name="MainBorder" Grid.Row="0" Background="#fff" BorderBrush="#fff" BorderThickness="0" CornerRadius="1,1,1,1"/>
<Button Margin="0,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Command="{Binding Path=TestCommand}"
Content="buttonText" />
</Grid>
</ControlTemplate>
<Style x:Key="ButtonPresenter" TargetType="{x:Type igDP:CellValuePresenter}">
<Style.Triggers>
<DataTrigger Binding="{Binding TestValue}" Value="Test">
<Setter Property="Template" Value="{StaticResource ButtonTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
and in the field layout, i have this:
<igDP:UnboundField Label="">
<igDP:Field.Settings>
<igDP:FieldSettings
CellValuePresenterStyle="{StaticResource ButtonPresenter}"
AllowResize="False"
CellMinWidth="16"
CellMaxWidth="16"
CellWidth="16"
LabelMinWidth="16"
LabelMaxWidth="16"
LabelWidth="16"
LabelHeight="0" />
</igDP:Field.Settings>
</igDP:UnboundField>
I get this error:
System.Windows.Data Error: 39 : BindingExpression path error: 'TestCommand' property not found on 'object' ''DataRecord' (HashCode=58216665)'. BindingExpression:Path=TestCommand; DataItem='DataRecord' (HashCode=58216665); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand')
I have also tried setting the relative source to itself, the TemplatedParent, and even the XamDataPresenter but all give the same error.
I'm just trying to get my button to fire the command. Any help?
Hello,
This is a common problem when using commands bindings in a control template. You have to bind to the ViewModel and not to templated parent or self. The binding will not always find it using FindAncestor mode. Here is one way to bind to the command through the template:
<Button Height="20" Tag="{Binding ElementName=vm, Path=MyCommand}"
Command="{Binding Tag, RelativeSource={RelativeSource Self}}">
Hi Alex,
With my understanding, I thought ElementName is used to bind to another Element in the xaml?
Can you give me an example of how to bind to the vm through the xaml?
<UserControl.DataContext> <local:MyViewModel x:Name="vm"/> </UserControl.DataContext>
where local is the namespace where you have defined the ViewModel.