尽管设置了数据上下文,但 SelectedItem 未知的数据上下文

SelectedItem unknown DataContext although datacontext is set

我有一个ListBox绑定了一个静态的ObservableCollection<HUDInfo>DataTrigger 显示以下错误 "PropertyPath | Cannot resolve property 'StatusConnection' in data context of type 'my window class'"。

                    <ListBox 
                        x:Name="ListBoxAvailableHuDs" 
                        ItemsSource="{Binding AvailableHUDs, Source={x:Static Core:HudModel.Current}}"
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="ListBoxItem">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding StatusConnection}" Value="CanDisconnect">
                                        <Setter Property="FontWeight" Value="Bold"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ListBox.ItemContainerStyle>
                    </ListBox>

Window 上我设置了以下 DataContext:

DataContext="{Binding RelativeSource={RelativeSource Self}}"

我的TextBlock也无法解析DataContext

                    <TextBlock 
                        Text="{Binding SelectedItem.DeviceId, ElementName=ListBoxAvailableHuDs}"/>

"Cannot resolve property 'DeviceId' in data context of type 'object'"

如何设置ListBoxDataContextHudInfo或者ListBoxItem使用的是DataContext

在运行时,每个列表框项目的数据上下文都得到了正确解析,但是,设计者无法做到这一点。您可以执行以下操作:

<Style TargetType="ListBoxItem" d:DataContext="{d:DesignInstance HudInfo }">
    <Style.Triggers>
    ...
    </Style.Triggers>
</Style TargetType="ListBoxItem" d:DataContext="{d:DesignInstance HudInfo">

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

我发现这会导致在设计器中显示错误 - 强调 d:DataContext 部分,但它至少允许它解析绑定。