2
votes

I have a listbox .list box bind with dataset.listbox binding is giving correct result I used checkbox in the list box for selection and that is working fine, but the problem is that when I am checked some item list box and scroll down the list box and checked another item go back above scrolldown then see some item randomly automatically unchecked. I don't want the item automatically unchecked. Please help me. I using this code below.

<DataTemplate x:Key="listBoxcontrycode">
    <StackPanel Margin="4">
        <DockPanel>
            <CheckBox Name="chkcntrycode" Content="{Binding userisd}"
                      Checked="chkcntrycode_Checked" Unchecked="Unchkcntrycode_Checked" />
        </DockPanel>
    </StackPanel>

<ListBox Height="89" HorizontalAlignment="Left" ItemTemplate="{StaticResource listBoxcontrycode}" ItemsSource="{Binding Tables[0]}" Margin="160,0,0,6" 
         Name="listcntrycode" VerticalAlignment="Bottom" Width="86" Grid.Column="3" Grid.ColumnSpan="2" Grid.RowSpan="2"                                       
         OverridesDefaultStyle="False" SelectionMode="Extended" IsEnabled="True" Grid.Row="3" />  

.

private void ListBoxBindingcntrycode()
{
    DBConnection ob = new DBConnection();
    RMS_Dataobject.getConnectionString = System.Configuration.ConfigurationSettings.AppSettings["EDM_RDMServer"];
    string commandString = "use [" + cmbEDM.SelectedItem.ToString() + "] select userisd from ADS_Audit_Log";
    DataTable dt = new DataTable();
    dt = ob.ReturnDatatable(commandString);
    DataSet ds = new DataSet();
    ds.Tables.Add(dt);
    listcntrycode.DataContext = ds;
}
2

2 Answers

1
votes

Try binding IsChecked to a boolean property. Right now the IsChecked is not saved anywhere so when the item is recycled that information is not saved.

1
votes

finally i have found the solution of this problem.i just used IsChecked property with two way mode binding.i have also add a column a virtual column.column name is 'ischecked' and am giving my updated code below.

<DataTemplate x:Key="listBoxcontrycode">     <StackPanel Margin="4">         <DockPanel>             <CheckBox Name="chkcntrycode" Content="{Binding userisd}"                       Checked="chkcntrycode_Checked" Unchecked="Unchkcntrycode_Checked" IsChecked="{Binding IsChecked, Mode=TwoWay} />         </DockPanel>     </StackPanel>  <ListBox Height="89" HorizontalAlignment="Left" ItemTemplate="{StaticResource listBoxcontrycode}" ItemsSource="{Binding Tables[0]}" Margin="160,0,0,6"           Name="listcntrycode" VerticalAlignment="Bottom" Width="86" Grid.Column="3" Grid.ColumnSpan="2" Grid.RowSpan="2"                                                 OverridesDefaultStyle="False" SelectionMode="Extended" IsEnabled="True">

   private void ListBoxBindingcntrycode()
        {
            DBConnection ob = new DBConnection();
            RMS_Dataobject.getConnectionString = System.Configuration.ConfigurationSettings.AppSettings["EDM_RDMServer"];
            string commandString = "use [" + cmbEDM.SelectedItem.ToString() + "] select distinct userisd ,CONVERT(bit,0) 'IsChecked' from ADS_Audit_Log order by CountryRMSCode";
            DataTable dt = new DataTable();
            dt = ob.ReturnDatatable(commandString);
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            listcntrycode.DataContext = ds;
        }