不调用DataTemplate中的命令

时间:2017-03-17 14:07:11

标签: c# .net wpf mvvm datatemplate

首先,我的View和ViewModel正确连接。但我无法在DataTemplate中完成绑定。

所以,我在DataTemplate中有Windows.Resources

<Window.Resources>
    <DataTemplate x:Key="DataGridHeader">
            <DockPanel>
                <TextBlock DockPanel.Dock="Top" TextAlignment="Left" Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}" Margin="5"/>
                <TextBox DockPanel.Dock="Top" BorderBrush="#3d3d3d" BorderThickness="1" Margin="5" Height="22">
                </TextBox>
                <Button DockPanel.Dock="Top" Height="30" Command="{Binding DataContext.OnFilterCommand, RelativeSource={RelativeSource AncestorType=Window}}"></Button>
            </DockPanel>
    </DataTemplate>
</Window.Resources>

然后,我有DataGrid

    <DataGrid x:Name="DataGridItems"
              Grid.Row="1"
              EnableRowVirtualization="True"
              ItemsSource="{Binding Items, Mode=TwoWay}" 
              RowHeight="25"
              RowHeaderWidth="0"
              VerticalContentAlignment="Center"
              HorizontalGridLinesBrush="#dddddd"
              VerticalGridLinesBrush="#dddddd"
              AlternatingRowBackground="#f2f2f2"
              AutoGenerateColumns="False"
              Background="#cecece"
              CanUserAddRows="False">
        <DataGrid.CellStyle>
            <Style TargetType="{x:Type DataGridCell}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridCell}">
                            <Grid Background="{TemplateBinding Background}">
                                <ContentPresenter VerticalAlignment="Center" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.CellStyle>         
        <DataGrid.ColumnHeaderStyle>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="Padding" Value="0" />
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="Foreground" Value="#111111"></Setter>
                <Setter Property="Background" Value="#cecece" />
                <Setter Property="BorderBrush" Value="#111111" />
                <Setter Property="BorderThickness" Value="0" />
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="SeparatorVisibility" Value="Visible"></Setter>
            </Style>
        </DataGrid.ColumnHeaderStyle>            
        <DataGrid.Columns>
            <DataGridTextColumn Header="Id" Binding="{Binding Path=Id}" HeaderTemplate="{StaticResource DataGridHeader}" Width="65"  />
            <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" HeaderTemplate="{StaticResource DataGridHeader}" Width="150" />
        </DataGrid.Columns>
    </DataGrid>

在我的ViewModel我有:

private ICommand _onLoadedCommand;
public ICommand OnFilterCommand => _onLoadedCommand ?? (_onLoadedCommand = new DelegateCommand(OnFilter));

    private void OnFilter()
    {
        Debug.WriteLine("Works!");
    }

并且它不起作用!我看着每个地方。我查看了这篇文章:http://nosalan.blogspot.com/2013/10/wpf-datagrid-with-filtering-mvvm.html

我认为我做的一切都是正确的。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

你什么都不缺。我测试了你的代码,它的工作原理!该命令将被执行。唯一的区别 - 我没有使用DelegateCommand,但我的实现,所以我不认为,这是一个原因。重启VS,重新启动计算机。它应该工作!