DataTemplate 中的 MVVM 命令

3

我在我的XAML中有一个如下的数据模板:

<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>

这是我的ViewModel的关键部分:

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();
}

以下是输出的错误信息:

System.Windows.Data Error: 4 : 找不到与引用'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')

我不明白为什么事件没有触发。有什么建议吗?

注意,文本框的属性已正确绑定。

编辑

以下是完整的控件:

<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>

以下是ViewModel类的定义:

public class OperativeSheetSelectionViewModel : ViewModelBase
{
     //
}

1
当您使用启用绑定调试的Visual Studio 2015时,输出窗口中是否显示任何内容? - weismat
你的 DataTemplate 是在哪里定义的?是在 UserControl 的资源中吗? - Tomtom
@Tomtom 是的,在 Window.Resources 标签中。 - Galma88
为什么需要RelativeSource?请展示整个标记,包括您试图引用的UserControl - dymanoid
2
请仔细阅读此链接。您需要指定要查找的祖先对象类型。 - dymanoid
显示剩余6条评论
1个回答

1

我做到了。 错误是在AncestorType上。我需要一个Window,而不是一个UserControl。(...)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接