在ListBox中使用复选框

时间:2014-04-22 16:45:55

标签: c# wpf itemtemplate

我有这个列表框: 这是我的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>

里面有一个复选框,ListBox由来自SQLite数据库的数据填充。我想在检查Checkbox时收到一条消息,显示ListBox的SelectedItem的名称。 这是我的CheckBox的属性

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

    }

当我检查我的复选框并单击此按钮时:

private void CheckedItem(object p)
    {

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

什么都没发生。 我已将Checkbox包含在我的SelectedItem中,如下所示:

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");
            }
        }
    }

问题出在哪里。

2 个答案:

答案 0 :(得分:2)

不是答案,而是评论太多了 这只是搞砸了 您需要阅读数据绑定并重新开始

以下get和set不引用相同的值

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

没有迹象SelectedItem绑定到任何东西

在CheckedItem上,它不是事件处理程序的签名

答案 1 :(得分:0)

像@Blam一样,通过为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");
}

其他两件事: