为什么绑定值为null时不调用Command?

时间:2015-01-28 13:09:39

标签: c# wpf mvvm command winrt-xaml

我创建了一个我想用null或空值启动的网格。 但是当我的textblocks binded值为null时,click / tap不会触发该命令。 当我有一个值它工作正常。 我还试图使用string.empty和" "哪个也不行。如果有人能够对此有所了解,那会很高兴。

XAML

<Grid x:Name="GridPuzzleOuter">
    <ItemsControl ItemsSource="{Binding Puzzle.Rows}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid Width="270">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Border Grid.Column="0">
                        <TextBlock Text="{Binding Cells[0].CellValue, Mode=TwoWay}">
                            <interactivity:Interaction.Behaviors>
                                <core:EventTriggerBehavior EventName="Tapped">
                                    <core:InvokeCommandAction
                                        Command="{Binding CellTappedCommand}" CommandParameter="{Binding Cells[0]}"/>
                                </core:EventTriggerBehavior>
                            </interactivity:Interaction.Behaviors>
                        </TextBlock>
                    </Border>
                    <Border Grid.Column="1">
                        <TextBlock Text="{Binding Cells[1].CellValue, Mode=TwoWay}">
                            <interactivity:Interaction.Behaviors>
                                <core:EventTriggerBehavior EventName="Tapped">
                                    <core:InvokeCommandAction
                                        Command="{Binding CellTappedCommand}" CommandParameter="{Binding Cells[1]}"/>
                                </core:EventTriggerBehavior>
                            </interactivity:Interaction.Behaviors>
                        </TextBlock>
                    </Border>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

行类

public class Row : ObservableObject
{
    public Cell[] Cells { get; set; }
    private RelayCommand<Cell> _cellTappedCommand;

    public RelayCommand<Cell> CellTappedCommand
    {
        get
        {
            return _cellTappedCommand ?? (_cellTappedCommand = new RelayCommand<Cell>((param) => Cellclick(param)));
        }
    }

    private void Cellclick(Cell param)
    {
        var vm = (new ViewModelLocator()).Main;
        vm.SelectedCell = param;
        // Do stuff...
   }
}

编辑: 通过将XAML更改为:

来解决
<Border Grid.Column="0">
    <interactivity:Interaction.Behaviors>
        <core:EventTriggerBehavior EventName="Tapped">
            <core:InvokeCommandAction
                    Command="{Binding CellTappedCommand}" CommandParameter="{Binding Cells[0]}"/>
        </core:EventTriggerBehavior>
    </interactivity:Interaction.Behaviors>
    <TextBlock Text="{Binding Cells[0].CellValue, Mode=TwoWay}">
    </TextBlock>
</Border>

1 个答案:

答案 0 :(得分:0)

只需将您的互动代码移至周围的Border,并确保将Background属性设置为Transparent。问题解决了。

//祝你好运!