Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Datatemplate中的MVVM命令_C#_Wpf_Events_Mvvm_Datatemplate - Fatal编程技术网

C# Datatemplate中的MVVM命令

C# Datatemplate中的MVVM命令,c#,wpf,events,mvvm,datatemplate,C#,Wpf,Events,Mvvm,Datatemplate,我的XAML中有如下数据模板: <DataTemplate x:Key="SheetToTemplate"> <TextBox Name="_txtToSheet" Text="{Binding Path=SHEET_TO, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Stretch"

我的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>
这是我在输出中得到的错误:

System.Windows.Data错误:4:找不到与绑定的源 参考“相对资源查找器”, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1'。 BindingExpression:Path=DataContext.FilterTextChangedCommand; DataItem=null;目标元素是“InvokeCommandAction” (HashCode=46858895);目标属性为“Command”(类型为“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>
我做到了。 错误发生在AncestorType上。我需要一个窗口,而不是用户控件。(……)


使用Visual Studio 2015(启用绑定调试)时,您是否在输出中看到任何内容?当然。我忘了。。。我正在编辑我的帖子你的
DataTemplate
定义在哪里?在窗口中的
UserControl
?@Tomtom yes的参考资料中。参考资料标签请仔细阅读。您需要指定要查找的祖先对象的类型。
<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>
public class OperativeSheetSelectionViewModel : ViewModelBase
{
     //
}