2016-01-20 79 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。 (...)