简体   繁体   English

WPF如何通过代码创建一个以画布为内容的数据模板?

[英]WPF How to Create a DataTemplate Which has Canvas As Content By Code?

I have the data template defined by XAML as below:我有 XAML 定义的数据模板,如下所示:

<DataTemplate x:Key="PointPushPinItemTemplate">
    <mapControl:MapItem mapControl:MapPanel.Location="{Binding}" >
        <Canvas>
            <Path StrokeThickness="2" Fill="Aqua">
                <Path.Data>
                    <EllipseGeometry RadiusX="10" RadiusY="10"/>
                </Path.Data>
            </Path>
        </Canvas>
    </mapControl:MapItem>
</DataTemplate>

I use below code to create it, but failed:我使用下面的代码来创建它,但失败了:

private DataTemplate PushPinPointDataTemplate()
{
    var pointItemFactory = new FrameworkElementFactory(typeof(MapItem));
    pointItemFactory.SetValue(MapPanel.LocationProperty, new Binding());
    var pointCanvas = new Canvas();
    pointCanvas.Children.Add(new Path
    {
        StrokeThickness = 2.0,
        Fill = new SolidColorBrush(Colors.Yellow),
        Data = new EllipseGeometry {RadiusX = 3, RadiusY = 3}
    });

    //below line always throw exception that FrameworkElementFactory.SetValue can't set a visual value
    pointItemFactory.SetValue(MapItem.ContentProperty, pointCanvas);
    return new DataTemplate
    {
        DataType = typeof(Location),
        VisualTree = pointItemFactory
    };
}

How to achieve this?如何实现这一目标? Previousely, I achieved this if there is no Canvas, but for this case, the Canvas to be the content of a content control.以前,我在没有 Canvas 的情况下实现了这一点,但对于这种情况,Canvas 是内容控件的内容。 ps: mapControl:MapItem is a UI element which inherited from ListBoxItem mapControl:MapPanel.Location is a attached property ps:mapControl:MapItem 是继承自 ListBoxItem 的 UI 元素 mapControl:MapPanel.Location 是附加属性

You need to create factories for the Canvas and the Path as well:您还需要为CanvasPath创建工厂:

private DataTemplate PushPinPointDataTemplate()
{
    var pointItemFactory = new FrameworkElementFactory(typeof(MapItem));
    pointItemFactory.SetValue(MapPanel.LocationProperty, new Binding("."));

    var pathFactory = new FrameworkElementFactory(typeof(Path));
    pathFactory.SetValue(Path.StrokeThicknessProperty, 2.0);
    pathFactory.SetValue(Path.FillProperty, Brushes.Yellow);
    pathFactory.SetValue(Path.DataProperty, new EllipseGeometry { RadiusX = 3, RadiusY = 3 });

    var pointCanvasFactory = new FrameworkElementFactory(typeof(Canvas));
    pointCanvasFactory.AppendChild(pathFactory);

    pointItemFactory.AppendChild(pointCanvasFactory);
    return new DataTemplate
    {
        DataType = typeof(Location),
        VisualTree = pointItemFactory
    };
}

Note that using a FrameworkElementFactory is a deprecated way to programmatically create templates though: https://msdn.microsoft.com/en-us/library/system.windows.frameworkelementfactory(v=vs.110).aspx .请注意,使用FrameworkElementFactory是一种不推荐使用的以编程方式创建模板的方法: https : //msdn.microsoft.com/en-us/library/system.windows.frameworkelementfactory(v=vs.110).aspx

The recommended way to programmatically create a template is to load XAML from a string or a memory stream using the Load method of the XamlReader class.以编程方式创建模板的推荐方法是使用XamlReader类的Load方法从string或内存流加载 XAML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM