Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在列表框中滚动时防止复选框中的onchecked事件_C#_Wpf_Checkbox_Scroll_Listbox - Fatal编程技术网

C# 在列表框中滚动时防止复选框中的onchecked事件

C# 在列表框中滚动时防止复选框中的onchecked事件,c#,wpf,checkbox,scroll,listbox,C#,Wpf,Checkbox,Scroll,Listbox,我在列表框中有一个复选框列表,列表框可以滚动,但每次我在列表框中向上或向下移动以查看其他复选框时,选中事件都会触发。有什么办法可以预防吗?我只是想在IsChecked更改时触发该事件 <ListBox Margin="0,5,0,0" Grid.Column="1" Background="Transparent" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollV

我在列表框中有一个复选框列表,列表框可以滚动,但每次我在列表框中向上或向下移动以查看其他复选框时,选中事件都会触发。有什么办法可以预防吗?我只是想在IsChecked更改时触发该事件

<ListBox Margin="0,5,0,0" Grid.Column="1" Background="Transparent" 
             ScrollViewer.HorizontalScrollBarVisibility="Disabled"
             ScrollViewer.VerticalScrollBarVisibility="Visible" 
             SelectionMode="Single" Name="lbPermission" BorderThickness="0"
             HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox IsEnabled="{Binding Path=IsEditable, Mode=OneWay, 
                    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Expander}}}" 
                              Validation.ErrorTemplate="{x:Null}" Content="{Binding Id}" 
                              Checked="CheckedHandler" Unchecked="CheckedHandler" >
                        <CheckBox.IsChecked>
                            <MultiBinding Converter="{StaticResource EnumConverter}" 
                                           Mode="OneWay">
                                <Binding Path="Id" />
                                <Binding Path="CurrentAccessList" 
                                         RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Expander}}" />
                            </MultiBinding>
                        </CheckBox.IsChecked>
                    </CheckBox>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
以下是转换器的代码:

public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {
            // The second value in the MultiBinding should be LaborAccountAccessList
            if ((value[1] as List<LaborAccountAccessList>) != null)
            {
                if (((IList)value[1]).Count > 0)
                    return (((List<LaborAccountAccessList>)value[1]).FirstOrDefault(x => x.PermissionId == value[0].ToString().ToLower()) != null) ?
                        !((List<LaborAccountAccessList>)value[1]).FirstOrDefault(x => x.PermissionId == value[0].ToString().ToLower()).IsObselete : false;
            }
            return false;
        }
        catch (Exception e)
        {
            ErrorLibrary.Instance.ErrorQueueContext.PrintError(String.Format("Unable bind the data to checkbox. Error={0}", e.ToString()));
            return false;
        }
    }
它的工作方式是这样的,有一个项目列表,我把它放在DataTemplate中创建这些复选框,然后使用Converter检查复选框(使用CurrentAccessList),如果特定的项目被检查与否。CurrentAccessList是来自数据库的列表

CurrentAccessList具有布尔属性


谢谢。

由于虚拟化,您遇到了这个问题。 要避免此行为,可以添加:

ScrollViewer.CanContentScroll="False" 
到你的列表框

但真正的问题在于枚举转换器。 虚拟化强制转换器在滚动时执行。 出于某种原因,它返回不同的值。
这就是调用处理程序的原因。

没有足够的代码来重现该问题。请更新问题。您希望我添加什么?用作DataContext的类和EnumConverter实现。您还可以解释一下为什么需要将名为CurrentAccessList的内容从扩展器传递到转换器吗?我很想帮你,但首先我需要重现这个问题。所以我需要尽可能多的补充。我希望它能有所帮助。我认为这一行-
在输出窗口中给了您一个绑定错误。是这样吗?
lbPermission.ItemsSource = PermissionList;
ScrollViewer.CanContentScroll="False"