用户控件不显示在窗口中

本文关键字:窗口 显示 控件 用户 | 更新日期: 2023-09-27 18:33:44

在这里和教程中多次搜索后,我空手而归,非常感谢帮助:

我有一个用户控件,其中包含其他类型的用户控件的列表。

我想在常规 WPF 窗口中显示此用户控件:

这是包含用户控件列表的用户控件:

<UserControl x:Class="UsersInfo.InfoLinesContainer"...
...
<Grid>
  <ListView ItemsSource="{Binding Path=InfoLineUC_ObservableList,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}">  
  </ListView>
</Grid>

在我的主窗口中,我做到了:

    <Window x:Class="UsersInfo.MainAppWindow"
    ...
            xmlns:usrCtr ="clr-namespace:UsersInfo"
    ...
    <usrCtr:InfoLinesContainer Grid.Row="11"
                               DataContext="{Binding Path=TheInfoLines, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" 
                               ScrollViewer.CanContentScroll="True" Margin="2" Grid.ColumnSpan="7" Grid.RowSpan="6" />
...

TheInfoLines 是包含其他用户控件列表的用户控件的实例

当我运行程序时,窗口出现时没有用户控件(我得到的只是一个空的方块)

我怎样才能让它工作?

用户控件不显示在窗口中

在主窗口中:

<usrCtr:InfoLinesContainer DataContext="{Binding Path=TheInfoLines, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" />

你想在这里绑定什么?您的窗口中似乎没有用户控件类型的祖先。