2016-01-20 81 views
2

我在XAML一个DataTemplate这样的:MVVM命令中的DataTemplate

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

这是我的视图模型与基本组成部分:

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 : 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')

我不明白为什么事件没有被解雇。 有什么建议吗?

ps。请注意文本框的属性被正确绑定。

编辑

这里完全控制

<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 
{ 
    // 
} 
+1

你使用Visual Studio 2015年(启用绑定调试)时,看到在输出什么? – weismat

+0

当然。我忘记了......我正在编辑我的帖子 – Galma88

+0

您的DataTemplate定义在哪里?在UserControl的资源中? – Tomtom

回答

0

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