在wpf中滚动列表框时,列表框选择随机取消选中某些项

在wpf中滚动列表框时,列表框选择随机取消选中某些项,wpf,listbox,Wpf,Listbox,我有一个列表框。列表框与数据集绑定。列表框绑定给出了正确的结果。我在列表框中使用了复选框进行选择,这很好,但问题是,当我选中某个项目列表框并向下滚动列表框并选中另一个项目时,返回到上面的滚动框,然后看到一些项目随机自动取消选中。我不希望该项自动取消选中。请帮帮我。我正在使用下面的代码 <DataTemplate x:Key="listBoxcontrycode"> <StackPanel Margin="4"> <DockPanel>

我有一个列表框。列表框与数据集绑定。列表框绑定给出了正确的结果。我在列表框中使用了复选框进行选择,这很好,但问题是,当我选中某个项目列表框并向下滚动列表框并选中另一个项目时,返回到上面的滚动框,然后看到一些项目随机自动取消选中。我不希望该项自动取消选中。请帮帮我。我正在使用下面的代码

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

尝试将IsChecked绑定到布尔属性。现在,IsChecked没有保存在任何地方,因此当项目被回收时,信息不会保存。

最后我找到了这个问题的解决方案。我刚刚使用了带有双向模式绑定的IsChecked属性。我还添加了一列虚拟列。列名为“IsChecked”,下面给出了我的更新代码

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

我14小时前的回答“尝试绑定已检查到布尔属性”如何?不是答案?我接受你的答案,先生!谢谢,节省了我的时间:)