【问题标题】:Only see the first referenced UserControl只看到第一个引用的 UserControl
【发布时间】:2015-01-26 10:00:18
【问题描述】:

怎么可能只有 WindowsFormsHost 中的 第一个 UserControl 显示在 GUI 中,而第二个仍然隐藏?

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wf_zoom0="clr-namespace:LowLightGrab;assembly=LowLightGrab" 
    xmlns:wf_zoom1="clr-namespace:LowLightGrab;assembly=LowLightGrab" 
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <WindowsFormsHost Height="154" HorizontalAlignment="Left" Name="wf_zoom0" VerticalAlignment="Top" Width="215" >
        <wf_zoom0:UserControl1/>
    </WindowsFormsHost>

    <WindowsFormsHost Height="161" HorizontalAlignment="Left" Margin="251,138,0,0" Name="wf_zoom1" VerticalAlignment="Top" Width="223" >
        <wf_zoom1:UserControl1/>
    </WindowsFormsHost>
</Grid>

【问题讨论】:

  • 删除您的保证金 Margin="251,138,0,0"
  • @eranotzap 为什么会有影响?
  • 因为两个都显示了,只是你把第二个推到右边251像素,所以你看不到。

标签: c# wpf xaml user-controls


【解决方案1】:

除了删除 Eran 添加的边距之外,您应该将 Rows 添加到 Grid 并将每个元素放置在自己的行中,如下所示:

<Window x:Class="ComboboxRectangles.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf_zoom0="clr-namespace:LowLightGrab;assembly=LowLightGrab" 
xmlns:wf_zoom1="clr-namespace:LowLightGrab;assembly=LowLightGrab" 
Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.RowDefinitions>
        <WindowsFormsHost Grid.Column="0" Height="154" HorizontalAlignment="Left" Name="wf_zoom0" VerticalAlignment="Top" Width="215" >
            <wf_zoom0:UserControl1/>
        </WindowsFormsHost>

        <WindowsFormsHost Grid.Column="1" Height="161" HorizontalAlignment="Left" Margin="251,138,0,0" Name="wf_zoom1" VerticalAlignment="Top" Width="223" >
            <wf_zoom1:UserControl1/>
        </WindowsFormsHost>
    </Grid>
</Window>

【讨论】:

  • 结果还是一样,它只显示第一个声明的 UserControl - 有或没有边距。它可能取决于 UserControl 本身吗?它是从相机读取的图像。
  • 您可以尝试放置一个按钮吗?
  • 按钮放在哪里?我可以在每一列中放置一个按钮,两个按钮都会显示,但只显示(第一个)WindowsFormsHost。
  • 所以第二个有问题。检查其内容。您也可以将其背景颜色设置为蓝色,然后查看是否可以看到。
  • 没错。当我为每个背景设置不同的颜色时,我可以看到两个背景。唯一没有出现的是第二个 UserControl。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-20
  • 2014-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-10
  • 2011-08-04
相关资源
最近更新 更多