我想在运行时在代码中执行以下操作:
<DataTemplate x:Key="lightGreenRectangle">
<Rectangle Fill="LightGreen"/>
</DataTemplate>
到目前为止,我已经:
public DataTemplate GetColouredRectangleInDataTemplate(Color colour)
{
DataTemplate dataTemplate = new dataTemplate();
return dataTemplate;
}
帮助?我知道这不是设计控件样式的最优雅方式,但我想指定颜色的组件有一个名为“PointTemplate”的属性,类型为DataTemplate。
答案 0 :(得分:25)
如果出于某种原因需要以编程方式创建DataTemplate
,您可以这样做:
XAML:
<Grid x:Name="myGrid">
<ContentControl ContentTemplate="{DynamicResource lightGreenRectangle}" />
</Grid>
代码中的某处:
public static DataTemplate CreateRectangleDataTemplate()
{
var rectangleFactory = new FrameworkElementFactory(typeof(Rectangle));
rectangleFactory.SetValue(Shape.FillProperty, new SolidColorBrush(System.Windows.Media.Colors.LightGreen));
return new DataTemplate
{
VisualTree = rectangleFactory,
};
}
public static void AddRectangleTemplateToResources(FrameworkElement element)
{
element.Resources.Add("lightGreenRectangle", CreateRectangleDataTemplate());
}
然后您只需将DataTemplate
添加到ResourceDictionary
即可使用。例如,在后面的代码中:
public MainWindow()
{
InitializeComponent();
AddRectangleTemplateToResources(myGrid);
}
希望这有帮助!
答案 1 :(得分:7)
使用以下助手类:
/// <summary>
/// Class that helps the creation of control and data templates by using delegates.
/// </summary>
public static class TemplateGenerator
{
private sealed class _TemplateGeneratorControl:
ContentControl
{
internal static readonly DependencyProperty FactoryProperty = DependencyProperty.Register("Factory", typeof(Func<object>), typeof(_TemplateGeneratorControl), new PropertyMetadata(null, _FactoryChanged));
private static void _FactoryChanged(DependencyObject instance, DependencyPropertyChangedEventArgs args)
{
var control = (_TemplateGeneratorControl)instance;
var factory = (Func<object>)args.NewValue;
control.Content = factory();
}
}
/// <summary>
/// Creates a data-template that uses the given delegate to create new instances.
/// </summary>
public static DataTemplate CreateDataTemplate(Func<object> factory)
{
if (factory == null)
throw new ArgumentNullException("factory");
var frameworkElementFactory = new FrameworkElementFactory(typeof(_TemplateGeneratorControl));
frameworkElementFactory.SetValue(_TemplateGeneratorControl.FactoryProperty, factory);
var dataTemplate = new DataTemplate(typeof(DependencyObject));
dataTemplate.VisualTree = frameworkElementFactory;
return dataTemplate;
}
/// <summary>
/// Creates a control-template that uses the given delegate to create new instances.
/// </summary>
public static ControlTemplate CreateControlTemplate(Type controlType, Func<object> factory)
{
if (controlType == null)
throw new ArgumentNullException("controlType");
if (factory == null)
throw new ArgumentNullException("factory");
var frameworkElementFactory = new FrameworkElementFactory(typeof(_TemplateGeneratorControl));
frameworkElementFactory.SetValue(_TemplateGeneratorControl.FactoryProperty, factory);
var controlTemplate = new ControlTemplate(controlType);
controlTemplate.VisualTree = frameworkElementFactory;
return controlTemplate;
}
}
您可以像这样创建数据模板:
DataTemplate template =
TemplateGenerator.CreateDataTemplate
(
() =>
{
var result = new TextBox()
result.SetBinding(TextBox.TextProperty, "BindingPathHere");
return result;
}
);
如果您直接创建控件而没有任何数据模板,则可以自由地使用任何代码来创建控件。 有关详细信息,请参阅代码项目中的此提示:http://www.codeproject.com/Tips/808808/Create-Data-and-Control-Templates-using-Delegates