【问题标题】:WPF - Rethinking control inheritanceWPF - 重新思考控件继承
【发布时间】:2010-11-14 22:39:00
【问题描述】:

我正在编写一个从 XML 文件加载记录并使用它们填充表单的应用程序。它是针对纸牌游戏的,所以我希望表单根据特定记录显示不同的字段并具有不同的外观(具体取决于记录中的“CardType”字段)。

在 vanilla C# 中,我将创建一个包含记录的基本窗口,然后为每个特定类型从它继承,从而更改窗口的视觉效果。然后我会检查类型,实例化正确的窗口并填充它。

在WPF中,不允许从windows继承,所以解决方法是使用单个窗口,然后根据情况使用样式/模板来调整窗口。

最初,我创建了完全独立的窗口,将适当的窗口传递给一个包含用于设置 DataContext 的记录数据的类。

但是,现在我正在努力做正确的事情,我正在努力掌握确切的方法。我尝试在包含用于保存我的字段的控件的样式中创建一个控件模板,并通过绑定来填充字段...

    <Style TargetType="{x:Type Label}" x:Key="testLabel">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Label}">
                    <Grid>
                        <Rectangle Fill="White" Stroke="Black" StrokeThickness="3" RadiusX="20" RadiusY="20" />
                        <DockPanel LastChildFill="False" Margin="10">
                            <DockPanel DockPanel.Dock="Top">
                                <Grid VerticalAlignment="Top" DockPanel.Dock="Left">
                                    <Ellipse Height="25" Width="25" Stroke="Black" StrokeThickness="2" Fill="Red"/>
                                    <TextBlock Name="textLevel" Foreground="Black" FontSize="15" TextAlignment="Center" VerticalAlignment="Center" Text="{Binding Level, UpdateSourceTrigger=PropertyChanged}"/>
                                </Grid>
                                <DockPanel DockPanel.Dock="Top">
                                    <TextBlock Name="textType" Foreground="Black" FontSize="15" FontWeight="Bold" FontStyle="Italic" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="10, 0, 10, 0" DockPanel.Dock="Right" Text="{Binding CardType, UpdateSourceTrigger=PropertyChanged}"/>       
                                    <TextBlock Name="textName" Foreground="Black" FontSize="20" FontWeight="Bold" Margin="10, 0, 10, 0" DockPanel.Dock="Left" Text="{Binding CardName, UpdateSourceTrigger=PropertyChanged}"/>      
                                </DockPanel>
                                <TextBlock Name="textPronounciation" Foreground="DarkSlateGray" FontSize="12" Margin="10, 0, 10, 0" DockPanel.Dock="Top" Text="{Binding Pronounciation, UpdateSourceTrigger=PropertyChanged}"/>
                            </DockPanel>
                            <Rectangle Name="rectPicture" Width="280" Height="210" Margin="0, 5" DockPanel.Dock="Top"/>
                            <TextBlock Name="textLineage" Foreground="Black" FontSize="15" Margin="10, 0, 10, 0" DockPanel.Dock="Top" Text="{Binding Lineage, UpdateSourceTrigger=PropertyChanged}"/>
                            <TextBlock Name="textFlavourText" Foreground="Black" FontSize="11" TextWrapping="Wrap" Margin="10, 0, 10, 0" DockPanel.Dock="Top" Text="{Binding FlavourText, UpdateSourceTrigger=PropertyChanged}"/>
                            <TextBlock Name="textQuote" Foreground="Black" FontSize="11" TextWrapping="Wrap" Margin="10, 5, 10, 0" DockPanel.Dock="Top" Text="{Binding Quote, UpdateSourceTrigger=PropertyChanged}"/>
                            <TextBlock Name="textAttribution" Foreground="Black" FontSize="9" FontWeight="Bold" TextWrapping="Wrap" HorizontalAlignment="Right" Margin="10, 0, 10, 0" DockPanel.Dock="Top" Text="{Binding Attribution, UpdateSourceTrigger=PropertyChanged}"/>
.
.
.

...但它没有填充。

控件模板中是否允许动态绑定,还是我应该做一些不同的事情?

(希望)所有相关代码如下:

来自调用函数:

    private void ShowCard(CardDetails record)
    {
        CardLayout layout = new CardLayout(record);
        CardWindow displayedCard = new CardWindow(layout);
        displayedCard.Show();
    }

CardLayout 类 - 由控件模板填充数据。我使用标签,因为它似乎是最简单的控件,因为无论如何我都要替换它的逻辑树:

<Label x:Class="Cards.CardLayout"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Style="{StaticResource testLabel}">
</Label>

.

public partial class CardLayout : Label
{
    public CardLayout()
    {
        InitializeComponent();
    }

    public CardLayout(CardDetails dataIn)
    {
        InitializeComponent();
        Initialise(dataIn);
    }

    public void Initialise(CardDetails dataIn)
    {
        this.DataContext = dataIn;
    }
}

CardWindow 类 - 将 CardLayout 保存为子类 - 目的是最终拥有不同的窗口来保存不同数量的 CardLayout。

<Window x:Class="Cards.CardWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <Grid Name="gridCard">

    </Grid>
</Window>

.

public partial class CardWindow : Window
{
    public CardWindow()
    {
        InitializeComponent();
    }

    public CardWindow(CardLayout dataIn)
    {
        InitializeComponent();
        Initialise(dataIn);
    }

    public void Initialise(CardLayout dataIn)
    {
        gridCard.Children.Add(dataIn);
    }
}

因此,当它运行时,我期待一个窗口包含一个网格,该网格包含一个标签,该标签已将其逻辑树替换为 ControlTemplate 并通过动态绑定填充。

抱歉这个漫无边际的问题,我不知道我是否以错误的方式做正确的事情,或者我应该放弃所有这些并以不同的方式处理它。

【问题讨论】:

  • 我建议您将 CardLayout 实现为 UserControl。这将更容易编辑。

标签: wpf inheritance binding


【解决方案1】:

在等待答案的同时,我简化了一切,从硬连线的值开始,然后逐渐添加一个步骤,直到我得到它的工作。现在我已经充实了这一切,我看不出我在做什么不同。

为了完整起见,这是我简化的 ControlTemplate,它适用于问题中的代码:

<Application.Resources>
    <Style TargetType="{x:Type Label}" x:Key="testWindow4">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Label}">
                    <Grid>
                        <TextBlock Text="{Binding CardName}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

其中 CardName 是 CardDetails 中的字符串属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-10
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 2021-10-30
    相关资源
    最近更新 更多