ContentControl未显示而未指定宽度/高度

时间:2012-11-19 03:36:52

标签: c# wpf contentcontrol

我在WPF项目中使用CefSharp。我正在将CefSharp.Wpf.WebView类添加到我的MainWindow.xaml中,如下所示:

_webView = new WebView(url, _settings);

我的XAML布局如下所示:

<Window x:Class="WPFContainer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test Project" Height="400" Width="930" Initialized="OnWindowInit" StateChanged="OnWindowStateChanged" Closing="OnWindowClose">
  <DockPanel Name="MainDockPanel" Height="400" Width="930">
    <Grid VerticalAlignment="Top" HorizontalAlignment="Left" ShowGridLines="True" Name="mainGrid" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Name="col1" Width="600" />
            <ColumnDefinition Name="col2" Width="330"/>
        </Grid.ColumnDefinitions>
    </Grid>
  </DockPanel>
</Window>

如果我删除Grid并直接将webView添加到DockPanel,则会显示正常。但是,如果我尝试在webView中将col1添加到Grid,则不会显示。如果我指定宽度/高度,它将显示在网格列中,但HorizontalAlignment.Stretch不起作用,我需要100%的宽度和高度!

我将webView添加到网格中的代码不起作用:

mainGrid.Children.Add(_webView);
Grid.SetColumn(_webView, 0);

1 个答案:

答案 0 :(得分:0)

问题是Children属性是在InitializeComponent中填充的。如果在初始化后将子项添加到网格中,则没有属性更改事件可添加到Children集合,因为它不是ObservableCollection。如果你要创建一个新的集合,我相信它会以编程方式工作,但是可以通过在xaml中定义你的webview来避免这种情况(前提是它有一个无表情的构造函数)。然后OnLoaded你可以注入网址和设置。

相关问题