我试图以编程的事件和元素添加到一个DataTemplate在Silverlight 3.0的应用程序。我有一个带有依赖项属性的用户控件,我想在其中设置模板,调整它,然后将修改后的版本设置为内部控件。如何以编程方式修改DataTemplate?
我的想法是采取进来的DataTemplate,读取其XAML,调整它,然后使用XamlReader创建一个修改后的DataTemplate,然后可以设置为内部控制。这种方法的问题是我不知道如何从originalal模板获得XAML例如(如果它甚至有可能):
protected virtual void OnItemTemplateChanged(DependencyPropertyChangedEventArgs e)
{
// Get the original Xaml from the set template
//string originalXaml = ???
// Modify the template
string newXaml = originalXaml.Replace("foo", "bar"); // for example
// Create a new template from the modified XAML
DataTemplate newTemplate = (DataTemplate)XamlReader.Load(newXaml);
// Update the inner template
this._childDropdown.ItemTemplate = newTemplate;
}
是否有人知道或者:1)如果有一种方式来阅读原始的XAML,或2)以编程方式修改DataTemplate的另一种方法。
感谢,
我(勉强)返工我的项目,所以它不需要DataTemplate中的操作。这不是我想要的,但至少我不会浪费任何时间来尝试这样做。谢谢,我希望未来的Silverlight版本允许对DataTemplate进行一些编程操作。 – 2009-12-15 16:56:30
我想我希望看到能够用自定义生成器替换ItemsControl ItemContainerGenerator,而我们现在不能使用它,所以我将不得不查看SL4,看看它是否允许。 – AnthonyWJones 2009-12-15 17:46:33