2013-09-27 82 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的內容屬性僅顯示標籤 –