WPF窗口不加载UserControl

时间:2013-11-21 17:32:40

标签: c# wpf user-controls

我想我有一个非常简单的问题。我正在通过contentcontrol显示一个usercontrol。 这样做是为了创建这个用户控件...

<UserControl x:Class="Exxeta.ModeldrivenCostEstimation.WPF.UserControlModelAnalysis"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="350" d:DesignWidth="525">
<Grid Margin="0,0,0,0">
    <Grid Background="#FFE5E5E5" HorizontalAlignment="Right" Width="489" Margin="0,93,11,0" Height="164" VerticalAlignment="Top" >
        <DataGrid x:Name="DataGrid" HorizontalAlignment="Left" VerticalAlignment="Top"
            Width="489" AutoGenerateColumns="False" ItemsSource="{Binding ViewModelModelAnalysis.GridObjects}" CanUserResizeRows="False" CanUserResizeColumns="False">
        </DataGrid>
    </Grid>


</Grid>

并在我的主窗口上声明了contentControl

<Window x:Class="WPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Test" Height="350" Width="525" ResizeMode="NoResize" Background="#FFE5E5E5">
    <ContentControl Name="Content"  Width="Auto" Opacity="1" Background="Transparent" ></ContentControl>
</Window>

..正如我读到的那样,我创建了这样的窗口。

public MainWindow(***)
{
    InitializeComponent();
    model = new BasicViewModel();
    UserControl c1 = new UserControlModelAnalysis();
    this.Content = c1;
    this.DataContext = model;

}

但是窗口没有显示内容。我错过了什么吗? 希望你能解决这个小问题。

感谢您的帮助 壹岐

5 个答案:

答案 0 :(得分:0)

尽管可以,但你不应该从代码后面启动它。以下是如何使用您的控件。 'xmlns:local'定义添加本地程序集,并允许您访问控件。

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication3"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <local:UserControl1></local:UserControl1>
    </Grid>
</Window>

...

<UserControl x:Class="WpfApplication3.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock>Test</TextBlock>
    </Grid>
</UserControl>

答案 1 :(得分:0)

这可以通过下面的代码来实现。您可以在那里进行用户控制,就像我将按钮添加到内容中一样。

<Window x:Class="WpfApplication4.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">
    <Grid>
        <ContentControl x:Name="Content"></ContentControl>
    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication4
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Loaded += MainWindow_Loaded;
        }

        private Button m_Button1;
        private Button m_Button2;

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Loaded -= MainWindow_Loaded;

            m_Button1 = new Button { Content = "Button1" };
            m_Button2 = new Button { Content = "Button2" };

            m_Button1.Click += button1_Click;
            m_Button2.Click += m_Button2_Click;

            Content.Content = m_Button1;
        }

        void m_Button2_Click(object sender, RoutedEventArgs e)
        {
            Content.Content = m_Button1;
        }

        void button1_Click(object sender, RoutedEventArgs e)
        {
            Content.Content = m_Button2;
        }
    }
}

答案 2 :(得分:0)

编辑:发生的事情是MainWindow.Content PUBLIC字段隐藏了基类Content属性。您正在将内容字段设置为UserControl,这也是一个ContentControl,因此没有类型转换错误。由于MainWindow.Content字段不是依赖项属性,因此UI永远不会更新,并且您的内容不会显示。

答案:重命名ContentControl并在控件上设置内容属性。

这有效。

XAML

<Window x:Class="Example_Project.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">
    <Grid>
        <ContentControl x:Name="currentContent"/>
    </Grid>
</Window>

背后的代码

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        currentContent.Content = new Content1();
    }
}

答案 3 :(得分:0)

感谢各位的回复。它现在正在运行。我基本上做的是将我的一个UserControle拖放到Window上并加载下一个像 this.currentContent.Content = new Content1()..就像我已经做过......并且它有效。我想踢什么说解释我的问题吧:-) 感谢

答案 4 :(得分:0)

我发现尝试在构造函数中显示Loaded事件时未引发该事件。这不是一个好主意。

public partial class MyDockpaneView : UserControl
    {
        public MyDockpaneView ()
        {
            InitializeComponent();
            Loaded += MyDockpaneView _Loaded;
            MyDockpaneViewModel.Show();
        }

除非将MyDockpaneView.Show()注释掉,否则不会调用MyDockpaneView _Loaded();