[WPF]如何使用代码创建DataTemplate(或者ControlTemplate)

1. 前言

上一篇文章([UWP]如何使用代码创建DataTemplate(或者ControlTemplate))介绍了在UWP上的情况,这篇文章再稍微介绍在WPF上如何实现。

2. 使用FrameworkElementFactory

FrameworkElementFactory用于以编程的方式创建模板,虽然文档中说不推荐,但WPF中常常使用这个类,例如DisplayMemberTemplateSelector

FrameworkElementFactory text = new FrameworkElementFactory(typeof(TextBlock));
Binding binding = new Binding
{
    Path = new PropertyPath("Name")
};
text.SetBinding(TextBlock.TextProperty, binding);

var xmlNodeContentTemplate = new DataTemplate();
xmlNodeContentTemplate.VisualTree = text;
xmlNodeContentTemplate.Seal();

ListControl.ItemTemplate = xmlNodeContentTemplate;

使用方式如上,这种方式可以方便地使用代码设置绑定或属性值,并且提供了AppendChild方法用于创建复杂的树结构。但是一旦这样做将使代码变得很复杂,建议还是不要这样做。

3. 使用XamlReader和XamlWriter

和UWP一样,WPF也支持使用XamlReader构建模板,只不过需要将

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

改为

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

和UWP不一样的是WPF还有XamlWriter这个工具。

XamlWriter提供一个静态 Save 方法,该方法可用于以受限的 XAML 序列化方式,将所提供的运行时对象序列化为 XAML 标记。如果使用这个类说不定可以用普通的方式创建一个UI元素并且最终创建它对应的DataTemplate,例如这样:

TextBlock text = new TextBlock();
Binding binding = new Binding("Name");
text.SetBinding(TextBlock.TextProperty, binding);
string xaml = string.Empty;
using (var stream = new MemoryStream())
{
    XamlWriter.Save(text, stream);
    using (var streamReader = new StreamReader(stream))
    {
        stream.Seek(0, SeekOrigin.Begin);
        xaml = streamReader.ReadToEnd();
    }
}

var template = (DataTemplate)XamlReader.Parse(@"
        <DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
                " + xaml + @"
</DataTemplate>");

但现实没有这么简单,在生成xaml的那步就出错了,声称的xaml如下:

<TextBlock Text="" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />

可以看到这段XAML并没有反映text.SetBinding(TextBlock.TextProperty, binding);这段设置的绑定。具体原因可见XamlWriter.Save 的序列化限制

值得庆幸的是WPF有足够长的历史,在这段历史里经过了无数人上上下下的折腾,上面提到的问题在10年前已经有人给出了解决方案:XamlWriter and Bindings Serialization

首先,MarkupExtension及其派生类(如Binding)需要有一个TypeConverter以便可以序列化:

internal class BindingConvertor : ExpressionConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(MarkupExtension))
            return true;
        else return false;
    }
    public override object ConvertTo(ITypeDescriptorContext context,
                                    System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(MarkupExtension))
        {
            BindingExpression bindingExpression = value as BindingExpression;
            if (bindingExpression == null)
                throw new Exception();
            return bindingExpression.ParentBinding;
        }

        return base.ConvertTo(context, culture, value, destinationType);
    }
}

然后,需要由TypeDescriptor告诉大家要使用这个TypeConverter:

internal static class EditorHelper
{
    public static void Register<T, TC>()
    {
        Attribute[] attr = new Attribute[1];
        TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC));
        attr[0] = vConv;
        TypeDescriptor.AddAttributes(typeof(T), attr);
    }
}

EditorHelper.Register<BindingExpression, BindingConvertor>();

然后就可以愉快地使用了:

Binding binding = new Binding("Name");
TextBlock text = new TextBlock();
text.SetBinding(TextBlock.TextProperty, binding);

StringBuilder outstr = new StringBuilder();
//this code need for right XML fomating 
XmlWriterSettings settings = new XmlWriterSettings
{
    Indent = true,
    OmitXmlDeclaration = true
};
var dsm = new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings))
{
    //this string need for turning on expression saving mode 
    XamlWriterMode = XamlWriterMode.Expression
};

XamlWriter.Save(text, dsm);

var xaml = outstr.ToString();

var template = (DataTemplate)XamlReader.Parse(@"
        <DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
                " + xaml + @"
</DataTemplate>");

这样就可以产生正确的XAML了:

<TextBlock Text="{Binding Path=Name}" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />

不过我没遇到这么复杂的业务需求,所以这个方案我也没实际使用过。从原文的评论来看果然还是有些问题,如ValidationRules不能正确地序列化。总之使用要谨慎。

4. 结语

有关TypeConverter和TypeDescriptor的更多信息可见我的另一篇文章了解TypeConverter。不过回顾了这篇文章后我发觉我更需要的是简化文章的能力,所以以后尽可能还是写简短实用些。

5. 参考

FrameworkElementFactory
XamlWriter
XamlWriter and Bindings Serialization
TypeConverter
TypeDescriptor
了解TypeConverter

转载于:https://www.cnblogs.com/dino623/p/Create-DataTemplate-Programatically-In-WPF.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要自定义WPF步骤条组件,可以使用WPF的控件库和自定义样式来实现。下面是实现的基本步骤: 1. 创建一个自定义控件,可以继承自WPF的控件库中的一种控件,如Control或UserControl。 2. 在控件中添加一个ItemsControl,用于显示步骤条的每个步骤。可以使用ItemsControl的ItemsSource属性来绑定一个集合,并使用ItemTemplate属性来定义每个步骤的外观。 3. 在ItemTemplate中添加一个Border或其他控件来显示步骤的背景。可以使用Trigger或MultiDataTrigger来根据步骤的状态(例如,已完成、进行中、未完成)来更改背景颜色或其他外观属性。 4. 可以为控件添加一些自定义属性和事件,以便更好地控制其行为。例如,可以添加一个CurrentStep属性来指定当前步骤,并使用它来更改步骤的状态。 5. 最后,可以将控件添加到自己的应用程序中,并使用它来显示步骤条。 下面是一个简单的示例代码,用于创建自定义步骤条控件: ```xaml <Style TargetType="{x:Type local:CustomStepBar}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:CustomStepBar}"> <ItemsControl ItemsSource="{TemplateBinding ItemsSource}"> <ItemsControl.ItemTemplate> <DataTemplate> <Border Width="20" Height="20" Margin="5" CornerRadius="10" Background="{Binding Path=Status, Converter={StaticResource StepStatusToBackgroundConverter}}"> <TextBlock Text="{Binding Path=StepNumber}" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border> </DataTemplate> </ItemsControl.ItemTemplate> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal" /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> </ControlTemplate> </Setter.Value> </Setter> </Style> ``` 此代码创建了一个自定义控件样式,用于将步骤条组件的外观定义为包含一系列带有文本的圆形边框。将Border的Background属性绑定到步骤的状态,并使用StepStatusToBackgroundConverter转换器来将状态转换为颜色。可以根据需要添加其他代码和样式来自定义控件的外观和行为。 希望这可以帮助你开始创建自己的WPF步骤条组件!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值