Reputation: 10234
I have created a class Account
.
Next, I have created another class ReorderWindowController
which has a field/property SelectedAccount
of type Account
.
Finally, I have written ReorderWindow
WPF window xaml file:
<Window ...
<Window.Resources>
<contollers:ReorderWindowController x:Key="WindowController" />
<DataTemplate DataType="{x:Type entities:Account}">
<Grid Width="140" Height="50" Margin="5">
<TextBlock Text="Some awesome text" />
<TextBlock Text="{Binding Name}" />
<TextBlock Text="Even more awesome text" />
</Grid>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid
Name="AccountGrid"
DataContext="{Binding Source={StaticResource ResourceKey=WindowController},
Path=SelectedAccount}">
</Grid>
</Grid>
</Window>
When I run my code, AccountGrid
is not showing anything. Why? How do I make object data bind to the Grid
and how do I make it use my data template? Thanks.
Upvotes: 5
Views: 10886
Reputation: 4621
Instead of a Grid use a ContentPresenter like this:
<Grid>
<ContentPresenter
Name="AccountGrid"
Content="{Binding Source={StaticResource ResourceKey=WindowController}, Path=SelectedAccount}">
</ContentPresenter>
</Grid>
Upvotes: 9