views:

303

answers:

0

Hi

I have the following listbox template within a WPF Page

I want the search button to only be enabled when a checkbox in the listbox has been checked. I am using the MVVM and have a seperate commands class Unfortunately the Canexecute method for my Search button is not being fired

Mycommand is defined as

 public static readonly RoutedUICommand SharePointSearch;
SharePointSearch = new RoutedUICommand("Run SharePoint Search...", "SharePointSearch", typeof(FISCommands));            

My XAML is as follows

ScrollViewer Height="Auto" Grid.Row="1" VerticalScrollBarVisibility="Auto">
        <ListBox Margin="10" ItemsSource="{Binding Path=SearchResults.Rows}">                        
            <ListBox.Template>
                <ControlTemplate>
                    <Border BorderBrush="Navy" BorderThickness="2">
                        <StackPanel>
                            <ItemsPresenter/>
                            <Button Width="200" Content="Search Sharepoint" 
                                    Command="s:FISCommands.SharePointSearch">                                    
                            </Button>
                        </StackPanel>                                
                    </Border>
                </ControlTemplate>
            </ListBox.Template>
            <ListBox.ItemTemplate>                    
                <DataTemplate>
                    <Grid>                            
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"></RowDefinition>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"></ColumnDefinition>                                
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>                                
                        <CheckBox Margin="15, 10, 10, 5" Grid.Column="0" Grid.Row="0" 
                                  Content="{Binding Path=Items[VALUE].Value}" 
                                  IsChecked="{Binding Path=IsSelected, Mode=TwoWay, 
                                  UpdateSourceTrigger=PropertyChanged}" >                                
                        </CheckBox>                                                                        
                        <TextBlock Margin="0, 10, 0, 5" Grid.Column="1" Grid.Row="0" 
                                   Text="{Binding Path=Items[CAPTION].Value}">                                
                        </TextBlock>
                    </Grid>
                </DataTemplate>                    
            </ListBox.ItemTemplate>                
        </ListBox>
    </ScrollViewer>

and my WiewModel is

protected ApplicationContext _context;

    public SharePointSearchViewModel(ApplicationContext context)
    {
        _context = context;

        SearchResults = context.SharePointSearchRowset;

        AddApplicationBinding(new CommandBinding(FISCommands.SharePointSearch, SharePointSearchExecuted, SharePointSearchCanExecuted));
    }

    public RowSet SearchResults { get; set; }

    public void SharePointSearchExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        foreach (Row item in SearchResults.Rows)
        {
            if (item.IsSelected)
            {
                //To Do build
            }
        }
    }

    public void SharePointSearchCanExecuted(object sender, CanExecuteRoutedEventArgs e)
    {
        foreach (Row item in SearchResults.Rows)
        {
            if (item.IsSelected)
            {
                e.CanExecute = true;
                return;
            }
        }

        e.CanExecute = false;
    }                      

I'm not sure what I am doing wrong I thought it was something to do with the binding on the check box but this seems to work

Thanks for any suggestions and help

Regards Colm