0

I have this List box: Here is the Code for my ListBox:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox x:Name="List" Grid.Row="0" ItemsSource="{Binding ShoppingItems}" FontSize="42" FontWeight="Light" FontFamily="Segoe WP Light" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Visible">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid Grid.Column="0" Margin="0,-15,0,22">
                            <Checkbox x:Name="MyCheckBox" IsChecked="{Binding IsChecked,Mode=TwoWay}"/>
                        </Grid>
                    <Grid x:Name="MyGrid" Margin="0,-15,0,22" Grid.Column="1">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <TextBlock Text="{Binding DisplayingItem}" FontSize="46" Grid.Column="0" Grid.Row="0" TextWrapping="Wrap" />
                        <TextBlock Text="quantity" FontSize="32" Grid.Row="1" Margin="6,0,0,0" Grid.Column="0" TextWrapping="Wrap"/>
                        <TextBlock Text="{Binding DisplayingQuantity}" FontSize="32" Grid.Column="1" Grid.Row="1" Margin="32,0,12,12" TextWrapping="Wrap"/>
                        <TextBlock Grid.Column="2" Grid.Row="1" FontSize="32" Margin="32,0,12,12" Text="{Binding DisplayingPackaging}" TextWrapping="Wrap"/>
                        <TextBlock Text="price" Margin="6,0,0,0" FontSize="32" Grid.Row="2" Grid.Column="0" TextWrapping="Wrap"/>
                        <TextBlock Text="$" FontSize="32" Grid.Row="2" Grid.Column="1" Margin="32,0,12,12" TextWrapping="Wrap"/>
                        <TextBlock Grid.Column="3" FontSize="32" Grid.Row="2"  Margin="32,0,12,12" Text="{Binding DisplayingPrice}" TextWrapping="Wrap"/>
                    </Grid>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

it has a checkbox inside, the ListBox is populated by data from a SQLite db. I want when I check the Checkbox to receive a message showing me the name of the SelectedItem of the ListBox. Here is the Property for my CheckBox

private bool isChecked;
    public bool  IsChecked
    {
        get
        {
             if(this.SelectedItem != null)
             {
                 return this.selectedItem.IsChecked;
             }
             return false;
        }
        set
        {
            this.isChecked = value;
        }

    }

When I check my Checkbox and Click This Button:

private void CheckedItem(object p)
    {

        if (this.IsChecked != false)
        {
            MessageBox.Show("Checked");
        }
    }

Nothing Happens. I have included my Checkbox in my SelectedItem Like this:

private ShowingModel selectedItem;
    public ShowingModel SelectedItem
    {
        get
        {
            return selectedItem;
        }
        set
        {
            if(this.selectedItem != value)
            {
                this.selectedItem = value;
                if (this.selectedItem != null)
                {
                    this.product = selectedItem.DisplayingItem;
                    this.price = selectedItem.DisplayingPrice;
                    this.qty = selectedItem.DisplayingQuantity;
                    this.package = selectedItem.DisplayingPackaging;
                    this.isChecked = selectedItem.IsChecked;
                }
                RaisePropertyChanged("MyPackage");
                RaisePropertyChanged("MyPrice");
                RaisePropertyChanged("MyProduct");
                RaisePropertyChanged("MyQty");
                RaisePropertyChanged("IsChecked");
            }
        }
    }

So Where is the Problem.

5
  • Help me - This Post contains very useful information. Please read it. Commented Apr 22, 2014 at 16:48
  • it is dependent to what do u mean by show message(MessageBox?), or you want to fire an event when checking the box? Commented Apr 22, 2014 at 16:54
  • I have posted more information. Commented Apr 22, 2014 at 17:01
  • pls check the output for any debugging error. Commented Apr 22, 2014 at 17:35
  • no Errors the app is running, im I doing anything wrong?? Commented Apr 22, 2014 at 17:42

2 Answers 2

2

Not an answer but too much for a comment
This is just messed up
You need to read up on data binding and start over

The following get and set don't refer to the same value

private bool isChecked;
public  bool IsChecked
{
    get
    {
         if(this.SelectedItem != null)
         {
             return this.selectedItem.IsChecked;
         }
         return false;
    }
    set
    {
        this.isChecked = value;
    }
}

No indication SelectedItem is bound to anything

On CheckedItem that is not the signature of an event handler

Sign up to request clarification or add additional context in comments.

Comments

0

Like @Blam said your immediate problem is resolveable by actually setting an event handler for the CheckBox "MyCheckBox."

<CheckBox x:Name="MyCheckBox" IsChecked="{Binding IsChecked,Mode=TwoWay}" 
Checked="MyCheckBox_Checked"/>

private void MyCheckBox_Checked(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Checked");
}

2 other things:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.