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



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

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

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

终于找到了这个问题的解决方案。我刚刚使用了具有双向模式绑定的 IsChecked 属性。我还在虚拟列中添加了一列。列名称是"ischecked",并在下面给出我更新的代码。

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

最新更新