Datatemplate中的MVVM命令

时间:2016-01-20 09:40:57

标签: c# wpf events mvvm datatemplate

我在我的XAML中有这样一个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错误:4:找不到绑定源   参考&#39; RelativeSource FindAncestor,   AncestorType =&#39; System.Windows.Controls.UserControl&#39 ;,   AncestorLevel =&#39; 1&#39;&#39 ;.   BindingExpression:路径= DataContext.FilterTextChangedCommand;   的DataItem = NULL;目标元素是&#39; InvokeCommandAction&#39;   (的HashCode = 46858895);目标财产是&#39; Command&#39; (键入&#39; ICommand&#39;)

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

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>

这里是ViewModel类的定义:

public class OperativeSheetSelectionViewModel : ViewModelBase
{
     //
}

1 个答案:

答案 0 :(得分:0)

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