2
votes

I have in my XAML a Datatemplate like this:

<DataTemplate x:Key="SheetToTemplate">
            <TextBox Name="_txtToSheet"
                    Text="{Binding Path=SHEET_TO, UpdateSourceTrigger=PropertyChanged}" 
                   HorizontalAlignment="Stretch" 
                   HorizontalContentAlignment="Center"
                   VerticalAlignment="Center"
                   Style="{StaticResource DigitOnlyTextBoxStyle}" >
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="TextChanged">
                        <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl, Mode=FindAncestor}, Path=DataContext.FilterTextChangedCommand }" >
                        </i:InvokeCommandAction>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </TextBox>
        </DataTemplate>

This is my viewmodel with essential part:

RelayCommand _filterTextChangedCommand;
public ICommand FilterTextChangedCommand
{
    get
    {
        if (_filterTextChangedCommand == null)
        {
            _filterTextChangedCommand = new RelayCommand(
                param => TextChange(param),
                param => true);
        }

        return _filterTextChangedCommand;
    }
}

private object TextChange(object param)
{
    throw new NotImplementedException();
}

This is the error I get in output:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:Path=DataContext.FilterTextChangedCommand; DataItem=null; target element is 'InvokeCommandAction' (HashCode=46858895); target property is 'Command' (type 'ICommand')

I don't understand why the event isn't fired. Any suggestion?

ps. Note that the property of the textbox is correctly bound.

EDIT

here the full control

<ListView Grid.Row="0"
                    ItemsSource="{Binding Path=SelectedOperations}"
                    Margin="5,10,5,5" 
                    Name="WorkOrders" 
                    SelectionMode="Single"
                    FontSize="13"
                    Background="AliceBlue"
                    BorderBrush="AliceBlue">

    <!--Style of items-->
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <!--Properties-->
            <Setter Property="Control.HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="Control.VerticalContentAlignment" Value="Center" />
            <!--Trigger-->
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="{x:Null}" />
                    <Setter Property="BorderBrush" Value="{x:Null}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>

    <ListView.View>
        <GridView >
            <GridViewColumn Header="Operation" CellTemplate="{StaticResource DetailIdenTemplate}"  Width="300"/>
            <GridViewColumn Header="From" CellTemplate="{StaticResource SheetFromTemplate}"  Width="50"/>
            <GridViewColumn Header="To" CellTemplate="{StaticResource SheetToTemplate}" Width="50" />
        </GridView>
    </ListView.View>
</ListView>

And here the ViewModel class definition:

public class OperativeSheetSelectionViewModel : ViewModelBase
{
     //
}
1
Do you see anything in Output when using Visual Studio 2015 (with enabled binding debugging)?weismat
Sure. I forgot it... I'm editing my postGalma88
Where is your DataTemplate defined? In the resources of the UserControl?Tomtom
@Tomtom yes in Window.Resources tagGalma88
Please read this carefully. You need to specify the type of ancestor object you want to find.dymanoid

1 Answers

0
votes

I did it. The error was on the AncestorType. I need a Window, not an UserControl. (...)