C# 在使用ItemsSource之前,Items集合必须为空

C# 在使用ItemsSource之前,Items集合必须为空,c#,wpf,xaml,listbox,datatrigger,C#,Wpf,Xaml,Listbox,Datatrigger,如果我将DataTrigger放在一个简单的列表框中,我会得到以下运行时异常: <ListBox ItemsSource="{Binding EdgedBoards}" SelectedItem="{Binding SelEdgedBoard, Mode=TwoWay}" DisplayMemberPath="Name"> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type List

如果我将DataTrigger放在一个简单的列表框中,我会得到以下运行时异常:

<ListBox ItemsSource="{Binding EdgedBoards}" SelectedItem="{Binding SelEdgedBoard, Mode=TwoWay}" DisplayMemberPath="Name">
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}" 
               BasedOn="{StaticResource {x:Type ListBoxItem}}">

            <Setter Property="IsSelected"
                    Value="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>
在使用ItemsSource之前,Items集合必须为空

不带datatrigger的我的列表框(无例外):


后一个代码有什么问题?

您将
样式添加为一项,但忘记了
列表框.Style
标记。由于您还试图绑定
项资源,因此会出现错误。

您将
样式添加为项,但忘记了
列表框.Style
标记。由于您还试图绑定
项资源,因此会出现错误。

您没有正确声明样式,因此它被设置为列表框的内容-您正在手动声明包含单个样式的列表


您应该用
元素包装现有的
样式
元素以解决此问题。

您没有正确声明样式,因此它被设置为列表框的内容-您正在手动声明包含单个样式的列表

您应该用
元素包装现有的
样式
元素以解决此问题

<ListBox ItemsSource="{Binding EdgedBoards}" SelectedItem="{Binding SelEdgedBoard, Mode=TwoWay}" DisplayMemberPath="Name">
    <Style TargetType="{x:Type ListBox}" BasedOn="{StaticResource {x:Type ListBox}}">
        <Setter Property="Focusable" Value="True" />

        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=EdgedBoardsAdd_UC, Path=Visibility}" Value="Visible">
                <Setter Property="Focusable" Value="False" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>