我試圖設置一個WPF DataTemplate將用於Line
(System.Windows.Shapes.Line
)對象。WPF DataTemplate失敗的形狀類型
從默認的.NET 4 WPF應用程序,我把我的窗口XAML到:
<Window x:Class="WpfTestDataTemplates.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type system:String}" >
<TextBlock>It's a string</TextBlock>
</DataTemplate>
<DataTemplate DataType="{x:Type Line}" >
<TextBlock>It's a line</TextBlock>
</DataTemplate>
</Window.Resources>
<ListView ItemsSource="{Binding MyItems}" />
</Window>
而後面的代碼是:
using System.Collections.Generic;
using System.Windows;
using System.Windows.Shapes;
namespace WpfTestDataTemplates
{
public partial class MainWindow : Window
{
public List<object> MyItems {get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
MyItems = new List<object>();
MyItems.Add("The first string");
MyItems.Add(new Line { X1 = 0, Y1 = 0, X2 = 5, Y2 = 5 });
MyItems.Add("The second string");
MyItems.Add(new Rectangle { Height = 5, Width = 15 });
MyItems.Add(42);
}
}
}
產生的窗口如下所示:
我期望第二個條目閱讀:它是一條線,但它看起來沒有找到Line
類型的DataTemplate。對於沒有顯式DataTemplate的類型,我期望默認渲染是對象的.ToString()成員,但這不是正在發生的事情。所以我期望第四個條目如下:System.Windows.Shapes.Rectangle
爲什麼{x:Type Line}
類型不被識別,以及DataTemplate應用於Shape對象的是什麼?
謝謝 - 有點幫助,我要如何使用它的樣式可能是足夠的。所以對於UIElements,WPF甚至不尋找DataTemplate?如果我想將線渲染爲矩形? – Govert
@Govert:您可能無法將'Line'呈現爲'Rectangle',但您可以讓自己的類_representing_一行並在該類上放置一個DataTemplate。我用一個例子更新了我的答案。 – Sphinxxx