-
June 23rd, 2009, 10:22 AM
#1
WPF auto-generate UserControls
Hi all,
I am developing an application in WPF and i need to create a UserControl whose layout and bindings will be created in Xaml file. When i run the program, it will fetch data from the database, create multiple user controls based on the returned rows and show all of the user controls on the window. In other words, each row will represent a new user control. I don't want to use datagrid or listview because returned data mustn't be displayed in rows.
Is there an efficient way to manage that?
Thanks in advance
-
June 24th, 2009, 08:30 PM
#2
Re: WPF auto-generate UserControls
A listview has the ability to display a usercontrol in each row.
Since you don't want to display the user control in row form, you can change out the default panel used for display.
What kind of layout of the user controls did you have in mind?
-
June 30th, 2009, 08:33 AM
#3
Re: WPF auto-generate UserControls
Thanks for the reply Arjay.
Layout of the control should contain some textboxes, textblocks etc. then i should bind the results gathered from the database to those textboxes and textblocks. How can i use that control in a listview?
Thanks in advance
-
June 30th, 2009, 11:19 AM
#4
Re: WPF auto-generate UserControls
You need to learn how to modify the ListView's default DataTemplate. I would recommend trying a few samples either on Msdn or on the internet.
Search google for "WPF listview DataTemplate tutorial" or "WPF listview DataTemplate samples"
-
July 1st, 2009, 03:37 AM
#5
Re: WPF auto-generate UserControls
Thanks again Arjay. I made some searchs about datatemplates and i tried this before. It works well if i define the listbox or listview inside the window. But if i create my own user control based on listbox or listview and when i create a datatemplate in the user control it only creates one item and binds the first row coming from the database to the template. My usercontrol is like below;
Code:
<ListView x:Class="ControlBinding.LVControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<ListView.ItemTemplate>
<DataTemplate>
<ContentPresenter/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
and in Window.xaml file i use it like;
Code:
<Window x:Class="ControlBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ControlBinding"
Title="Window1" Height="408" Width="562">
<Grid>
<local:LVControl x:Name="LV" Width="Auto" Height="Auto" DataContext="{Binding}" >
<Border Margin="0,0,0,0" CornerRadius="10" BorderThickness="5" BorderBrush="MediumPurple" MaxWidth="400" MaxHeight="250">
<WrapPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<WrapPanel Orientation="Horizontal">
<TextBlock Text="Title:" Width="150"/>
<TextBox Text="{Binding Path=Title}" Width="200"/>
</WrapPanel>
<WrapPanel Orientation="Horizontal">
<TextBlock Text="Name:" Width="150"/>
<TextBox Text="{Binding Path=Name}" Width="200"/>
</WrapPanel>
<WrapPanel Orientation="Horizontal">
<TextBlock Text="Surname:" Width="150"/>
<TextBox Text="{Binding Path=Surname}" Width="200"/>
</WrapPanel>
<WrapPanel Orientation="Horizontal">
<TextBlock Text="Position:" Width="150"/>
<TextBox Text="{Binding Path=Position}" Width="200"/>
</WrapPanel>
</WrapPanel>
</Border>
</local:LVControl>
</Grid>
</Window>
And this results in only one listviewitem and one row. If i change the DataContext="{Binding}" to ItemsSource="{Binding}" it throws Xaml Parse Exception.
What am i doing wrong?
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|