本文整理汇总了C#中System.Windows.DataTemplate类的典型用法代码示例。如果您正苦于以下问题:C# DataTemplate类的具体用法?C# DataTemplate怎么用?C# DataTemplate使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DataTemplate类属于System.Windows命名空间,在下文中一共展示了DataTemplate类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateItemTemplate
private DataTemplate CreateItemTemplate()
{
var template = new DataTemplate();
template.VisualTree = new FrameworkElementFactory(typeof(StackPanel));
var txtBlock = new FrameworkElementFactory(typeof(TextBlock));
txtBlock.SetBinding(TextBlock.TextProperty, new Binding("Street1"));
template.VisualTree.AppendChild(txtBlock);
txtBlock = new FrameworkElementFactory(typeof(TextBlock));
txtBlock.SetBinding(TextBlock.TextProperty, new Binding("City"));
template.VisualTree.AppendChild(txtBlock);
var trigger = new MultiDataTrigger();
trigger.Conditions.Add(
new Condition(
new Binding("Cty"), //misspelling
"Tallahassee"
)
);
trigger.Conditions.Add(
new Condition(
new Binding("StateOrProvince"),
"Florida"
)
);
template.Triggers.Add(trigger);
return template;
}
开发者ID:ssethi,项目名称:TestFrameworks,代码行数:34,代码来源:UIBoundToCustomerWithContentControlAndMultiTriggers.cs
示例2: LoadContent
/// <summary>
/// Loads content in the specified template.
/// </summary>
/// <param name="template">Template to load.</param>
/// <returns>Contents loaded from the specified template.</returns>
public static DependencyObject LoadContent(DataTemplate template)
{
// This is a work around for a Beta1 Bug
// Where under load the LoadContent can fail.
// This stops us seeing this issue as frequently.
// The idea is to try upto 5 times to load a template.
// If it still does not load then rethrow the exception.
int retryCount = 5;
DependencyObject dependencyObject = null;
if (template != null)
{
while (null == dependencyObject && 0 != retryCount)
{
try
{
dependencyObject = template.LoadContent();
}
catch (System.Windows.Markup.XamlParseException e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
System.Diagnostics.Debug.WriteLine("\n Template Load Error");
retryCount--;
System.Threading.Thread.SpinWait(2000);
if (retryCount == 0)
{
throw;
}
}
}
}
return dependencyObject;
}
示例3: SelectTemplate
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
NGTreeNode node = item as NGTreeNode;
if (node != null)
{
if (node.Tag is NamedColor)
{
if (null == _namedColorTemplate)
_namedColorTemplate = (DataTemplate)_parent.TryFindResource("NamedColorTemplate");
if (null != _namedColorTemplate)
return _namedColorTemplate;
}
else if (node.Tag is IColorSet)
{
if (null == _colorSetTemplate)
_colorSetTemplate = (DataTemplate)_parent.TryFindResource("ColorSetTemplate");
if (null != _colorSetTemplate)
return _colorSetTemplate;
}
else
{
if (null == _treeOtherTemplate)
_treeOtherTemplate = (DataTemplate)_parent.TryFindResource("TreeOtherTemplate");
if (null != _treeOtherTemplate)
return _treeOtherTemplate;
}
}
return base.SelectTemplate(item, container);
}
示例4: ListViewSorter
public ListViewSorter(ListView listView, DefaultHeaderSortDirections defaultHeaderSortDirections, DataTemplate upArrow, DataTemplate downArrow)
{
this.listView = listView;
this.defaultHeaderSortDirections = defaultHeaderSortDirections;
this.upArrow = upArrow;
this.downArrow = downArrow;
}
示例5: PropertyValueDialogControl
// <summary>
// Basic ctor
// </summary>
// <param name="property">Property to display</param>
// <param name="valueDialogTemplate">Template to use</param>
public PropertyValueDialogControl(PropertyEntry property, DataTemplate valueDialogTemplate)
{
Fx.Assert(property != null, "property parameter is null");
Fx.Assert(valueDialogTemplate != null, "valueDialogTemplate parameter is null");
ModelPropertyEntry modelPropertyValue = property as ModelPropertyEntry;
if (modelPropertyValue != null)
{
_rootTransaction = modelPropertyValue.FirstModelProperty.Value.BeginEdit();
}
InitializeComponent();
// Make sure we use PI-specific resources within this control
this.Resources.MergedDictionaries.Add(PropertyInspectorResources.GetResources());
// Hook into an opening of nested dialogs. PropertyInspector class takes care of this for us.
// However, we are using Blend's collection editor which doesn't do the same thing, so
// we need to reproduce that behavior manually.
PropertyValueDialogHost.AttachOpenDialogHandlers(this);
// Hook into the standard set of Commands
_defaultCommandHandler = new PropertyValueEditorCommandHandler(this);
_OKButton.Click += new RoutedEventHandler(OnOkButtonClicked);
_cancelButton.Click += new RoutedEventHandler(OnCancelButtonClicked);
_contentControl.Content = property.PropertyValue;
_contentControl.ContentTemplate = valueDialogTemplate;
//Handle the commit and cancel keys within the property inspector, that is hosted in the collection editor
ValueEditorUtils.SetHandlesCommitKeys(this, true);
}
示例6: EditableTextBlock
public EditableTextBlock()
{
var textBox = new FrameworkElementFactory(typeof(TextBox));
textBox.SetValue(Control.PaddingProperty, new Thickness(1)); // 1px for border
textBox.AddHandler(FrameworkElement.LoadedEvent, new RoutedEventHandler(TextBox_Loaded));
textBox.AddHandler(UIElement.KeyDownEvent, new KeyEventHandler(TextBox_KeyDown));
textBox.AddHandler(UIElement.LostFocusEvent, new RoutedEventHandler(TextBox_LostFocus));
textBox.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("Text") { Source = this, Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
var editTemplate = new DataTemplate { VisualTree = textBox };
var textBlock = new FrameworkElementFactory(typeof(TextBlock));
textBlock.SetValue(FrameworkElement.MarginProperty, new Thickness(2));
textBlock.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(TextBlock_MouseDown));
textBlock.SetBinding(TextBlock.TextProperty, new System.Windows.Data.Binding("Text") { Source = this });
var viewTemplate = new DataTemplate { VisualTree = textBlock };
var style = new System.Windows.Style(typeof(EditableTextBlock));
var trigger = new Trigger { Property = IsInEditModeProperty, Value = true };
trigger.Setters.Add(new Setter { Property = ContentTemplateProperty, Value = editTemplate });
style.Triggers.Add(trigger);
trigger = new Trigger { Property = IsInEditModeProperty, Value = false };
trigger.Setters.Add(new Setter { Property = ContentTemplateProperty, Value = viewTemplate });
style.Triggers.Add(trigger);
Style = style;
}
示例7: TryFindDataTemplate
protected abstract bool TryFindDataTemplate(
ResourceDictionary resources,
FrameworkElement resourcesOwner,
DependencyObject itemContainer,
object item,
Type itemType,
out DataTemplate dataTemplate);
示例8: LoadDataTemplate
public static FrameworkContentElement LoadDataTemplate(DataTemplate dataTemplate)
{
var content = dataTemplate.LoadContent();
if (content is Fragment)
{
return ((Fragment)content).Content;
}
else if (content is TextBlock)
{
var inlines = ((TextBlock)content).Inlines;
if (inlines.Count == 1)
return inlines.FirstInline;
else
{
var paragraph = new Paragraph();
// we can't use an enumerator, since adding an inline removes it from its collection
while (inlines.FirstInline != null)
paragraph.Inlines.Add(inlines.FirstInline);
return paragraph;
}
}
else
{
throw new Exception("Data template needs to contain a <Fragment> or <TextBlock>");
}
}
示例9: TrySelectTemplate
public bool TrySelectTemplate(
object item,
DependencyObject container,
string context,
bool isTooltip,
out DataTemplate dataTemplate)
{
dataTemplate = null;
if (DataTemplateProvider == null)
return false;
if (item == null)
return false;
var cached_item = (object)null;
if(Item.TryGetTarget(out cached_item))
{
if (cached_item == null)
return false;
if (cached_item.GetType() != item.GetType())
return false;
}
if (!string.Equals(Context, context))
return false;
return DataTemplateProvider.TrySelectTemplate(item, container, context, isTooltip, out dataTemplate);
}
示例10: LoadDataTemplate
internal static FrameworkContentElement LoadDataTemplate(DataTemplate dataTemplate)
{
object content = dataTemplate.LoadContent();
if (content is Fragment)
{
return (FrameworkContentElement)((Fragment)content).Content;
}
else if (content is TextBlock)
{
InlineCollection inlines = ((TextBlock)content).Inlines;
if (inlines.Count == 1)
{
return inlines.FirstInline;
}
else
{
Paragraph paragraph = new Paragraph();
while (inlines.FirstInline != null)
{
paragraph.Inlines.Add(inlines.FirstInline);
}
return paragraph;
}
}
else
{
throw new Exception("Data template needs to contain a <Fragment> or <TextBlock>");
}
}
示例11: AddDataTemplate
private void AddDataTemplate(Type viewType, Type viewModelType)
{
var dataTemplate = new DataTemplate(viewModelType);
dataTemplate.VisualTree = new FrameworkElementFactory(viewType);
dataTemplate.Seal();
App.Current.Resources.Add(new DataTemplateKey(viewModelType), dataTemplate);
}
示例12: SelectTemplate
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
DataTemplate tmp = this.Template1;
this.Template1 = this.Template2;
this.Template2 = tmp;
return tmp;
}
示例13: CreateDefaultTemplate
internal static DataTemplate CreateDefaultTemplate()
{
var dataTemplate = new DataTemplate();
var factory = new FrameworkElementFactory(typeof(ContentPresenter));
dataTemplate.VisualTree = factory;
return dataTemplate;
}
示例14: KernelOnComponentRegistered
private void KernelOnComponentRegistered(string key, IHandler handler)
{
Type implementation = handler.ComponentModel.Implementation;
if (implementation.Namespace == null ||
!implementation.Namespace.EndsWith("ViewModels"))
{
return;
}
var viewTypeName = implementation.Namespace.Replace("ViewModels", "Views") + "." +
implementation.Name.Replace("ViewModel", "View");
var qualifiedViewTypeName = implementation.AssemblyQualifiedName.Replace(implementation.FullName, viewTypeName);
Type viewType = Type.GetType(qualifiedViewTypeName, false);
if (viewType == null)
{
throw new InvalidOperationException();
}
var dt = new DataTemplate {DataType = viewType};
var viewFactory = new FrameworkElementFactory(viewType);
dt.VisualTree = viewFactory;
_resources.Add(new DataTemplateKey(implementation), dt);
}
示例15: ListColorsEvenElegantlier
public ListColorsEvenElegantlier()
{
Title = "List Colors Even Elegantlier";
DataTemplate template = new DataTemplate(typeof(NamedBrush));
FrameworkElementFactory factoryStack = new FrameworkElementFactory(typeof(StackPanel));
factoryStack.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
template.VisualTree = factoryStack;
FrameworkElementFactory factoryRectangle = new FrameworkElementFactory(typeof(Rectangle));
factoryRectangle.SetValue(Rectangle.WidthProperty, 16.0);
factoryRectangle.SetValue(Rectangle.HeightProperty, 16.0);
factoryRectangle.SetValue(Rectangle.MarginProperty, new Thickness(2));
factoryRectangle.SetValue(Rectangle.StrokeProperty, SystemColors.WindowTextBrush);
factoryRectangle.SetBinding(Rectangle.FillProperty, new Binding("Brush"));
factoryStack.AppendChild(factoryRectangle);
FrameworkElementFactory factoryTextBlock = new FrameworkElementFactory(typeof(TextBlock));
factoryTextBlock.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center);
factoryTextBlock.SetValue(TextBlock.TextProperty, new Binding("Name"));
factoryStack.AppendChild(factoryTextBlock);
ListBox lstbox = new ListBox();
lstbox.Width = 150;
lstbox.Height = 150;
Content = lstbox;
lstbox.ItemTemplate = template;
lstbox.ItemsSource = NamedBrush.All;
lstbox.SelectedValuePath = "Brush";
lstbox.SetBinding(ListBox.SelectedValueProperty, "Background");
lstbox.DataContext = this;
}