2013-11-21 44 views
0

我想我有一个非常简单的问题。我正在通过contentcontrol绑定显示一个用户控件。 做什么是创建该用户控件...WPF Window无法加载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; 

} 

但是内容不显示在窗口中。我错过了那里的东西吗? 希望你能帮助我摆脱这个小问题。

感谢您的帮助 壹岐

回答

0

编辑:发生什么事是MainWindow.Content PUBLIC字段隐藏基类Content属性。您正在将内容字段设置为您的UserControl,该用户控件也是ContentControl因此没有类型转换错误。因为MainWindow.Content字段不是依赖项属性,所以UI永远不会更新,并且不会显示您的内容。

回答:重命名您的ContentControl并设置控件的内容属性。

This Works。

的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(); 
    } 
} 
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> 
+0

我想要建立的东西就像一个wizzard behavoir。我可以点击下一页。我不应该从代码背后启动它。这很容易为什么要建立这样的东西 –

+0

所以想要与新的内容交换控制内容? –

+0

我所拥有的是我的MainWindows,它是一个普通的WPF窗口。我也宣布了一个内容控制。我还宣布了一些用户控件与不同的UI元素。如果我启动WPF窗口,则应显示第一个用户控件。在“下一步”点击secound usercontrol应该显示等等..我希望它看起来像一个法师wizzard .. –

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; 
     } 
    } 
} 
0

感谢所有的你的阶跃响应。它现在运行。我基本上做的是将其中一个UserControles拖放到窗口上,并加载下一个,如 this.currentContent.Content = new Content1()..就像我已经做过的那样。我想那是什么踢说解释我的问题正确:-) 谢谢