A multi-platform .NET UI framework. -!-!-!-!- VOTE FOR SUPPORT IN RIDER: https://youtrack.jetbrains.com/issue/RIDER-39247 -!-!-!-!-
listBox.DataTemplates = new DataTemplates
{
new DataTemplate<Person>(o => new Button() { Content = o.Name}),
};
listBox.DataTemplates = new DataTemplates
{
new DataTemplate(o => new Button() { Content = ((Person)o).Name}),
};
Match
function which is called to check if the DataTemplate matches the data
DataTemplate<Person>
it automatically generates the Match
function for you
Match
so the DataTemplate
is matching anything
ListBox
is a ScrollViewer
which contains the ItemsPresenter
, i.e. ScrollViewer.Content = new ItemsPresenter()
Content
property, a DataTemplate
is applied
DataTemplate
that matches ItemsPresenter
DataTemplate
and asks it "do you handle ItemsPresenters?" and your DataTemplate
says "Yes I do!"
ItemsPresenter
in and bang :fire:
does that make sense?
Aha! Now it has sense!
<ListBox.DataTemplates>
<XamlDataTemplate DataType="SomeType" >
....
</XamlDataTemplate>
</ListBox.DataTemplates>
<Window Width="300" Height="300" xmlns:m="model">
<ListBox Items="{Binding Path=People}">
<ListBox.DataTemplates>
<XamlDataTemplate>
<Button Content="Hello world!" />
</XamlDataTemplate>
</ListBox.DataTemplates>
</ListBox>
</Window>
Match = data =>
{
if (DataType == null)
{
return !(data is Control);
}
return DataType == data.GetType();
};
ItemsControl
has an ItemTemplate
property
ListBox
etc have a known type
<Window Width="300" Height="300" xmlns:m="model">
<ListBox Items="{Binding Path=People}">
<ListBox.DataTemplates>
<XamlDataTemplate Type="Person">
<Button Content="Hello world!" />
</XamlDataTemplate>
</ListBox.DataTemplates>
</ListBox>
</Window>
<ContentControl>
<ContentControl.Resources>
<DataTemplate DataType="{x:Type local:Item}">
<Label>WHAT?</Label>
</DataTemplate>
</ContentControl.Resources>
<local:Item Value="Hello"/>
</ContentControl>
<ContentControl>
<ContentControl.Resources>
<DataTemplate DataType="sys:String">
<Label>WHAT?</Label>
</DataTemplate>
</ContentControl.Resources>
<sys:String>Hello</sys:String>
</ContentControl>
<ContentControl>
<ContentControl.Resources>
<DataTemplate DataType="Label">
<Label>WHAT?</Label>
</DataTemplate>
</ContentControl.Resources>
<Label>Hello</Label>
</ContentControl>
MaterializeDataTemplate
add this
if (data is Control)
{
return (Control)data;
}
ContentControl
templates TabItem
to display the tab content instead of the tab control
<Window Width="300" Height="300" xmlns:m="model">
<StackPanel>
<TabControl>
<TabItem Header="One" />
<TabItem Header="Two" />
<TabItem Header="Three" />
</TabControl>
</StackPanel>
</Window>