有的时候在XAML里面创建一个通用的DataTemplate比较麻烦,例如在DataTemplate里面有一个控件需要绑定到不同的属性里面,例如下面的代码:
<DataTemplate x:Key="TestDataTemplate"> <TextBox Width="200" Text="{Binding Path=XXX}" /> </DataTemplate>
<ListView x:Name="TestDataGrid"> <ListView.View> <GridView> <GridViewColumn Header="Name" CellTemplate="{StaticResource TestDataTemplate}"/> <GridViewColumn Header="Gender" CellTemplate="{StaticResource TestDataTemplate}"/> <GridViewColumn Header="Birthday" CellTemplate="{StaticResource TestDataTemplate}"/> </GridView> </ListView.View> </ListView> |
你希望重用TestDataTemplate,里面的TextBox的Binding的路径可以根据应用的GridViewColumn而改变。这种情况下,我能想到的方法可能就是使用代码的方式来创建DataTemplate了,使用代码的方式给你很大的可定制性—虽然MSDN不推荐使用代码的方式创建DataTemplate(http://msdn.microsoft.com/en-us/library/system.windows.frameworkelementfactory.aspx)。
下面是通过代码的方式创建DataTemplate的方法:
C#代码:
/// <summary> |
/// Interaction logic for Window1.xaml |
/// </summary> |
public partial class Window1 : Window |
{ |
public Window1() |
{ |
InitializeComponent(); |
|
Loaded += new RoutedEventHandler(Window1_Loaded); |
} |
|
void Window1_Loaded(object sender, RoutedEventArgs e) |
{ |
People people = new People(); |
people.Add(new Person("Person 1", false, new DateTime(1980, 1, 30))); |
people.Add(new Person("Person 2", false, new DateTime(1980, 1, 30))); |
people.Add(new Person("Person 3", true, new DateTime(1982, 1, 30))); |
people.Add(new Person("Person 4", true, new DateTime(1980, 4, 30))); |
people.Add(new Person("Person 5", true, new DateTime(1980, 1, 3))); |
people.Add(new Person("Person 6", false, new DateTime(1977, 11, 10))); |
people.Add(new Person("Person 7", true, new DateTime(1987, 12, 30))); |
people.Add(new Person("Person 8", false, new DateTime(1980, 1, 1))); |
people.Add(new Person("Person 9", false, new DateTime(1981, 2, 28))); |
people.Add(new Person("Person 10", false, new DateTime(1983, 10, 30))); |
|
TestDataGrid.ItemsSource = people; |
|
GridViewColumn column1 = ((GridView)TestDataGrid.View).Columns[0]; |
GridViewColumn column2 = ((GridView)TestDataGrid.View).Columns[1]; |
GridViewColumn column3 = ((GridView)TestDataGrid.View).Columns[2]; |
column1.CellTemplate = CreateTemplate("Name"); |
column2.CellTemplate = CreateTemplate("Gender"); |
column3.CellTemplate = CreateTemplate("Birthday"); |
} |
|
private DataTemplate CreateTemplate(string column) |
{ |
DataTemplate template = new DataTemplate(); |
|
FrameworkElementFactory factoryText = |
new FrameworkElementFactory(typeof(TextBox)); |
factoryText.SetValue(TextBox.TextProperty, new Binding(column)); |
template.VisualTree = factoryText; |
|
return template; |
} |
} |
|
public class Person |
{ |
public string Name { get; set; } |
|
public bool Gender { get; set; } |
|
public DateTime Birthday { get; set; } |
|
public Person(string name, bool gender, DateTime birthday) |
{ |
Name = name; |
Gender = gender; |
Birthday = birthday; |
} |
} |
|
public class People : ObservableCollection<Person> |
{ |
} |
XAML代码:
<ListView x:Name="TestDataGrid"> |
<ListView.View> |
<GridView> |
<GridViewColumn Header="Name" /> |
<GridViewColumn Header="Gender" /> |
<GridViewColumn Header="Birthday"/> |
</GridView> |
</ListView.View> |
</ListView> |
如果你需要给DataTemplate里面的控件设置Style或者Template的话,可以通过
factoryText.SetValue(..., ....);
来实现