1

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

在使用 ItemsSource 之前,项目集合必须为空

我的没有数据触发器的列表框(也不例外):

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

我的列表框DataTrigger

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

后面的代码有什么问题?

4

2 回答 2

7

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

您应该用元素包装现有Style元素<ListBox.Style>以解决此问题。

于 2012-07-25T14:59:34.780 回答
3

您添加了Style 作为项目,您忘记了ListBox.Style标签。由于您还尝试绑定,因此ItemsSource您会收到错误消息。

于 2012-07-25T14:59:13.220 回答