張願麗
張願麗

Reputation: 17

WPF checkbox binding don't work

I've updated the question with my xaml code. The problem is the listview, which is under a popup tag. Thank everyone for your help.

Xaml code

<Popup Name="black_list_seting" 
                   StaysOpen="False"
              AllowsTransparency="True"
                   Closed="emoj_closed"
    PlacementTarget ="{Binding ElementName=emj_btn}" 
    Placement="Left">
                <Border  HorizontalAlignment="Left" VerticalAlignment="Top"  BorderBrush="Black" BorderThickness="0.5">
                    <Grid x:Name="black_list_grid" Width="270" Height="238" Background="White" ScrollViewer.VerticalScrollBarVisibility="Visible"  >

                        <ListView  x:Name="black_listview" HorizontalAlignment="Left" Height="219"  Margin="10,10,0,0" VerticalAlignment="Top" Width="254" >

                            <ListView.View>

                                <GridView >

                                    <GridView.ColumnHeaderTemplate>
                                        <DataTemplate >
                                            <DockPanel Margin="-40,0,0,0">

                                                <TextBlock FontSize="12" HorizontalAlignment="Left" Foreground="#383e4b">
                                                    <TextBlock.Text>
                                                        <Binding/>
                                                    </TextBlock.Text>
                                                </TextBlock>
                                            </DockPanel>
                                        </DataTemplate>
                                    </GridView.ColumnHeaderTemplate>

                                    <GridViewColumn Header="name"
                DisplayMemberBinding="{Binding user_name}"
                Width="100"/>
                                    <GridViewColumn Header="ignore"
                Width="100">

                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <CheckBox DataContext="{Binding check_key}"  IsChecked="{Binding IsSelected}" Checked="black_list_item_fn"></CheckBox>
                                            </DataTemplate>

                                        </GridViewColumn.CellTemplate>

                                    </GridViewColumn>


                                </GridView>
                            </ListView.View>






                        </ListView>



                    </Grid>
                </Border>
            </Popup>

Code behind

    public class contact_user_data
    {

        public string user_name { get; set; }
        public string user_key { get; set; }
        public string check_key { get; set; }
        public string head_url { get; set; }
        private bool isSelected;
        private string name;

        public bool IsSelected { get; set; }
    }

List view Itemssource definition code

         var source = global_var.black_list_soruce[md5];
        if (source != black_listview.ItemsSource)
        {

        }
        black_listview.ItemsSource = null;
        //  black_listview.Items.Clear();
        black_listview.ItemsSource = source;

        black_list_seting.IsOpen = true;

No matter how I define the check box binding, it has been unchecked.

Please help me !! I spent three days researching this problem

Upvotes: 0

Views: 701

Answers (1)

Ilan
Ilan

Reputation: 2772

looking at next expression:

<CheckBox DataContext="{Binding check_key}"  IsChecked="{Binding IsSelected}" Checked="black_list_item_fn"></CheckBox>

I can say you that the binding can't work. Please pay attention on a small explanation:

  1. A DataContext="{Binding check_key}" - defines the data context of the checkbox.
  2. A IsChecked="{Binding IsSelected}" - say that the checked/unchecked state is defined by the IsSelected property which is existing in the DataContext. Since the check_key property of the contact_user_data class is a string and the IsSelected is not a member of the string class, we have a binding expression that destruct the binding process, and nothing is working here(the check box wasn't bound because of binding expression).
  3. In addition you should use an INPC here to make a binding to work on the fly.

My suggestion is; try to remove the DataContext="{Binding check_key}" from your checkbox declaration, then the framework will find the binding path and the state will be defined at least in first time(since the INPC is not implemented- interesting article concerning the INPC must).

Example

<CheckBox IsChecked="{Binding IsSelected}" Checked="black_list_item_fn"/>

Regards.

Upvotes: 1

Related Questions