2013-09-27 51 views
0

我有一种情况下,我需要创建一个用户控件托管内容演示者。现在,内容演示者应该使用模板来显示数据。用户控件与更改内容模板显示内容

我设计了一个用户控件,如下所示。 XAML

<UserControl x:Class="Dashboard.ComponentStatisticsControl" 
     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" 
     Name="SatisticsControl" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <Label Content="{Binding ElementName=SatisticsControl, Path=Title}" 
      Grid.Row="0" 
      Background="SkyBlue"/> 
     <ContentPresenter Content="{Binding ElementName=SatisticsControl, Path=AdditionalContent}" 
         Grid.Row="1"/> 
    </Grid> 
</UserControl> 

现在,我在我的MainWindow.xaml定义一个WrapPanel应承载ComponentStatisticsControl

<Window x:Class="Dashboard.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:Dashboard" 
    Height="350" Width="525" 
    ShowInTaskbar="True" 
    WindowState="Maximized" 
    WindowStyle="None" 
    Name="_this"> 
    <Window.Resources> 
     <LinearGradientBrush x:Key="PanelBackground" 
         StartPoint="0, 1" 
         EndPoint="1, 0"> 
      <GradientStop Color="SkyBlue" Offset="0.3"/> 
      <GradientStop Color="PaleGreen" Offset="1"/> 
     </LinearGradientBrush> 
     <SolidColorBrush x:Key="BorderBrush" 
        Color="Blue"/> 
    </Window.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 
     <StackPanel Grid.Row="0"> 
      <Button Click="Button_Click" 
       HorizontalAlignment="Left" 
       VerticalAlignment="Center">Click</Button> 
     </StackPanel> 
     <WrapPanel Name="WrapPanelMain" 
       Orientation="Horizontal" 
       FlowDirection="LeftToRight" 
       Grid.Row="1"> 
     </WrapPanel> 
    </Grid> 
</Window> 

现在我在代码中创建的ComponentStatisticsControl后面的内容。

public void CreateComponent(ref DiscoveryMessage Message) 
    { 
     switch (Message.Identifier) 
     { 
      case ComponentIdentifier.Dispatcher: 
       { 
        ComponentStatisticsControl StatisticsControl = new ComponentStatisticsControl(); 
        StatisticsControl.Title = "Dispatcher"; 
        StatisticsControl.AdditionalContent = new Label() { Content = "Hello"}; 
        WrapPanelMain.Children.Add(StatisticsControl); 
        break; 
       } 
     } 
    } 

但是,我看不到添加的数据。我错过了什么。我花了很多时间敲错了什么地方出了问题。

我应该能够看到在WrapPanel标签“你好”的内容集。

public class DispatcherStatistics 
{ 
    private uint f_QCount; 

    public uint QueueCount { get { return f_QCount; } 
     set 
     { 
      f_QCount = value; 
     } 
    } 

} 

我将这个类实例设置为AdditionalContent。所以,只要我分配这个类的新实例,QueueCount就会被更新。

在此先感谢

编辑 我得到的包装板我的班级类型的文本。现在上述问题已解决,但我怎样才能定义一个模板来显示内容。

public void CreateComponent(ref DiscoveryMessage Message) 
    { 
     1switch (Message.Identifier) 
     { 
      case ComponentIdentifier.Dispatcher: 
       { 
        ComponentStatisticsControl StatisticsControl = new ComponentStatisticsControl(); 
        StatisticsControl.Title = "Dispatcher"; 
        StatisticsControl.AdditionalContent = f_clDispatcherStatistics; 
        WrapPanelMain.Children.Add(StatisticsControl); 
        break; 
       } 
     } 
    } 

f_clDispatcherStatistics是DispatcherStatistics类 这显示 “Dashboard.DispatcherStatistics”

私人实例变量我要像

QueueCount显示的东西:0

这样的格式。

+1

内容=“{结合的ElementName = SatisticsControl < - - 这是正确拼写吗?可以这是错误吗? – sexta13

回答

0

你这样做有点复杂。您可以直接使用UserControlContent属性。

这里是模板的(ContentPresenter使用默认值)的重要组成部分:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <Label Content="{Binding ElementName=SatisticsControl, Path=Title}" 
     Grid.Row="0" 
     Background="SkyBlue"/> 
    <ContentPresenter Grid.Row="1"/> 
</Grid> 

然后直接使用Content属性:

ComponentStatisticsControl StatisticsControl = new ComponentStatisticsControl(); 
StatisticsControl.Title = "Dispatcher"; 
StatisticsControl.Content = new Label() { Content = "Hello"}; 
WrapPanelMain.Children.Add(StatisticsControl); 
+0

我纠正了我的用户控制与你建议的一个请看我上面的编辑谢谢 –

+0

我不能跟着了如果最初的问题解决好,如果你有一个新的,创建一个新的问题。 – meilke

+0

@melike设置内容仅显示标签。我认为我已经声明的AdditionalContent应该存在。由于UserControl的内容属性仅显示标签 –