命令中的ListBox项

时间:2015-08-20 10:53:30

标签: wpf xaml

我试图在用户点击按钮时获取Listboxitem。但在我的命令中,该项始终为null。请帮助我的命令

    <DataTemplate x:Key="MyTemplate">
        <Grid ScrollViewer.HorizontalScrollBarVisibility="Disabled" Margin="5" >                  
            <Button Command="{Binding MyCommand}" CommandParameter="{Binding ElementName=lb, Path=SelectedItem}">
            </Button>
        </Grid>
    </DataTemplate>

    <ListBox Name="lb" Grid.Row="1"
            ScrollViewer.HorizontalScrollBarVisibility="Disabled"
            ItemsSource="{Binding MyItemsList}"  
            Margin="5"
            HorizontalContentAlignment="Stretch" 
            ItemTemplate="{StaticResource MyTemplate}" />

ViewModel代码

    public ICommand MyCommand
    {
        get
        {
            if (_MyCommand == null)
                _MyCommand = new RelayCommand(o => MyCommand_Execute(o));
            return _MyCommand;
        }
    }

    private void MyCommand_Execute(object item)
    {
      //           item is always null
    }

0 个答案:

没有答案