用户控件,具有用于显示内容的内容模板的更改

本文关键字:显示 控件 用于 用户 | 更新日期: 2023-09-27 18:33:06

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

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

<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>

现在,我正在代码隐藏中创建组件统计控件的内容。

    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 中看到标签"Hello"的内容集。

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

我将把这个类实例设置为附加内容。这样,每当我分配此类的新实例时,都会更新 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 类的私有实例变量这将显示"仪表板.调度程序统计信息">

我想显示类似的东西

队列计数 : 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);