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

时间:2012-07-04 17:22:33

标签: wpf listbox

我有一个列表框.list框绑定与dataset.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;
}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

最后我找到了这个问题的解决方案。我只是使用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;
        }
相关问题