Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用户控件,用于更改用于显示内容的内容模板_C#_Wpf_Xaml - Fatal编程技术网

C# 用户控件,用于更改用于显示内容的内容模板

C# 用户控件,用于更改用于显示内容的内容模板,c#,wpf,xaml,C#,Wpf,Xaml,在这种情况下,我需要创建一个承载内容演示者的用户控件。现在,内容演示者应该使用模板来显示数据 我设计了一个用户控件,如下所示。xaml <UserControl x:Class="Dashboard.ComponentStatisticsControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.c

在这种情况下,我需要创建一个承载内容演示者的用户控件。现在,内容演示者应该使用模板来显示数据

我设计了一个用户控件,如下所示。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>
但是,我看不到添加的数据。我错过了什么。我已经花了很多时间来唠叨出了什么问题

我应该能够在包装中看到标签“Hello”的内容集

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.DispatchersStatistics”

我想展示像这样的东西

队列计数:0


喜欢这种格式。

你做的有点复杂。您可以直接使用
UserControl
Content
属性

以下是模板的重要部分(使用默认的
ContentPresenter
):


内容=”{Binding ElementName=SatisticsControl我用你建议的方法更正了我的UserControl。请看我上面的编辑。谢谢,我不能再继续了。如果最初的问题解决了,很好。如果你有一个新问题,创建一个新问题。@melike设置内容只显示标签。我认为我声明的其他内容应该列表。由于UserControl的content属性仅显示labelIt,因此如果您甚至不使用
内容
,则labelIt没有意义使用
附加内容。如果您需要多个元素,请将其包装在容器中或使用多个控件。如果您有多个标签,请使用字符串属性并在模板中绑定到它们。
public class DispatcherStatistics
{
    private uint f_QCount;

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

}
    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;
                }
        }
    }
<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>
ComponentStatisticsControl StatisticsControl = new ComponentStatisticsControl();
StatisticsControl.Title = "Dispatcher";
StatisticsControl.Content = new Label() { Content = "Hello"};
WrapPanelMain.Children.Add(StatisticsControl);