Tech_Andrew


i'm trying to build sort of a quizing engine. So i have this object called Quiz, that has a title and other things. but also the quiz object has a Collection object of questions called QuestionCollection and within that QuestionCollection i have another collection called QuestionChoicesCollection object, that hold the possible choices for each question.

so is it possible that i can render my quiz object with datetemplating. do i have to like.. include a listbox or something in my quiz's datatemplate and place the listbox's itemteplate to what i want the questionchoicescollection to be rendered as. i would do this but i didn't want to just like select a question or something, i just want it all to be rendered out with all the questions and a list of radiobuttons under each question and the radiobuttons would maybe try to do a trigger on when the selected radio button changes and then it will set a property of the question object to define the selected answer for the question.

i just want like each of the question choices to be rendered as RadioButtons with their text next to them. but since i don't know how many possible number of question and choices of each question i'm runn
ing to into a speed bump... what do u think


Re: Windows Presentation Foundation (WPF) Wpf DataTemplate Question

Ramanujam Sampath

Hi Andrew,

You dont need to know the count of items to to set it up to a DataTemplate. Set up the DataSource of the ListBox to the Object Collection while you set the ListBoxItem Template to the DataTemplate that you will be creating.

This will help you to produce so much control as necessary as the DataSource and also the Template of those wil be on the ListBoxItem.

Here is a small example for my OpenDialogBox Control where I dont know how much files or folders will be listed:

XAML File

<ListView x:Name="lstFileList" Width="460" Margin="3,0,0,0" Height="196" FontFamily="Arial" FontSize="10" ItemsSource="{Binding ElementName=OpenDialogControl, Path=STRFileList, Mode=OneWay}"

SelectionMode="Extended">

<ListView.View>

<GridView>

<GridViewColumn Header="Name" Width="200"

CellTemplateSelector="{StaticResource myDataTemplateSelector}"/>

<GridViewColumn Header="Type"

CellTemplateSelector="{StaticResource myTypeTemplateSelector}"/>

<GridViewColumn Header="Size"

CellTemplate="{StaticResource SizeDataTemplate}"/>

</GridView>

</ListView.View>

</ListView>

Here I have a lot of DataTemplate and hence I use a DataTemplate Selector to select one of the template depending upon the Data. The object 'STRFileList' is derived from an XML file which has all the files and folder listings.

Hoppe this helps.

~Ram






Re: Windows Presentation Foundation (WPF) Wpf DataTemplate Question

Carole Snyder

Hm. I'd try to use an HeaderItemsControl. If you can post a small repro of your object model (so I don't have to guess it), I'll try to whip out the DataTemplate you could use.






Re: Windows Presentation Foundation (WPF) Wpf DataTemplate Question

Ramanujam Sampath

Hi,

See the example below.

This is the DataTemplate Selector with the DataTemplate which is a part of my <Windows.Resources>. The bold text in this are the ones which are different Data Templates.

Data Template Selector along with different Data Templates

<DList:TaskListDataTemplateSelector x:Key="myDataTemplateSelector"/>

<DList:TaskTypeDataTemplateSelector x:Key="myTypeTemplateSelector"/>

<DataTemplate x:Key="SizeDataTemplate" DataType="{x:Type GridViewColumn}">

<TextBlock Text=""/>

</< FONT>DataTemplate>

<DataTemplate x:Key="WordDocTypeDataTemplate" DataType="{x:Type GridViewColumn}">

<TextBlock Text="Word Document"/>

</< FONT>DataTemplate>

<DataTemplate x:Key="WordDocDataTemplate" DataType="{x:Type GridViewColumn}">

<StackPanel Orientation="Horizontal">

<StackPanel.ContextMenu>

<ContextMenu>

<MenuItem Header="Explore"/>

<Separator/>

<MenuItem Name="miDelete" Tag="Delete" Header="Delete"/>

<MenuItem Name="Rename" Header="Rename"/>

<MenuItem Name="miOpen" Tag="Open" Header="Open..."/>

<MenuItem Name="miEdit" Tag="Edit" Header="Edit.."/>

<Separator/>

<MenuItem Name="miProperties" Tag="Properties" Header="Properties.."/>

<MenuItem Name="miArrange" Header="Arrange"></< FONT>MenuItem>

</< FONT>ContextMenu>

</< FONT>StackPanel.ContextMenu>

<Image Source="..\..\Resources\Image\Doc Types\WordMainIconImage.png"/>

<TextBlock Text="{Binding Mode=OneWay}"/>

</< FONT>StackPanel>

</< FONT>DataTemplate>

<DataTemplate x:Key="FolderTypeDataTemplate" DataType="{x:Type GridViewColumn}">

<TextBlock Text="Folder"/>

</< FONT>DataTemplate>

<DataTemplate x:Key="FolderDataTemplate" DataType="{x:Type GridViewColumn}">

<StackPanel Orientation="Horizontal">

<StackPanel.ContextMenu>

<ContextMenu>

<MenuItem Header="Explore"/>

<Separator/>

<MenuItem Name="miDelete" Tag="Delete" Header="Delete"/>

<MenuItem Name="Rename" Header="Rename"/>

<MenuItem Name="miOpen" Tag="Open" Header="Open..."/>

<MenuItem Name="miEdit" Tag="Edit" Header="Edit.."/>

<Separator/>

<MenuItem Name="miProperties" Tag="Properties" Header="Properties.."/>

<MenuItem Name="miArrange" Header="Arrange"></< FONT>MenuItem>

</< FONT>ContextMenu>

</< FONT>StackPanel.ContextMenu>

<Image Source="..\..\Resources\Image\Doc Types\CreateNewFolder.png"/>

<TextBlock Text="{Binding Mode=OneWay}"/>

</< FONT>StackPanel>

</< FONT>DataTemplate>

<DataTemplate x:Key="TextFileTypeDataTemplate" DataType="{x:Type GridViewColumn}">

<TextBlock Text="Text File"/>

</< FONT>DataTemplate>

<DataTemplate x:Key="TextFileDataTemplate" DataType="{x:Type GridViewColumn}">

<StackPanel Orientation="Horizontal">

<StackPanel.ContextMenu>

<ContextMenu>

<MenuItem Header="Explore"/>

<Separator/>

<MenuItem Name="miDelete" Tag="Delete" Header="Delete"/>

<MenuItem Name="Rename" Header="Rename"/>

<MenuItem Name="miOpen" Tag="Open" Header="Open..."/>

<MenuItem Name="miEdit" Tag="Edit" Header="Edit.."/>

<Separator/>

<MenuItem Name="miProperties" Tag="Properties" Header="Properties.."/>

<MenuItem Name="miArrange" Header="Arrange"></< FONT>MenuItem>

</< FONT>ContextMenu>

</< FONT>StackPanel.ContextMenu>

<Image Source="..\..\Resources\Image\Doc Types\txt.png"/>

<TextBlock Text="{Binding Mode=OneWay}"/>

</< FONT>StackPanel>

</< FONT>DataTemplate>

<DataTemplate x:Key="PowerPointTypeDataTemplate" DataType="{x:Type GridViewColumn}">

<TextBlock Text="PowerPoint Document"/>

</< FONT>DataTemplate>

<DataTemplate x:Key="PowerPointDataTemplate" DataType="{x:Type GridViewColumn}">

<StackPanel Orientation="Horizontal">

<StackPanel.ContextMenu>

<ContextMenu>

<MenuItem Header="Explore"/>

<Separator/>

<MenuItem Tag="Delete" Header="Delete"/>

<MenuItem Name="Rename" Header="Rename"/>

<MenuItem Tag="Open" Header="Open..."/>

<MenuItem Tag="Edit" Header="Edit.."/>

<Separator/>

<MenuItem Tag="Properties" Header="Properties.."/>

<MenuItem Header="Arrange"></< FONT>MenuItem>

</< FONT>ContextMenu>

</< FONT>StackPanel.ContextMenu>

<Image Source="..\..\Resources\Image\Doc Types\PowerpointMainIconImage.png"/>

<TextBlock Text="{Binding Mode=OneWay}"/>

</< FONT>StackPanel>

</< FONT>DataTemplate>

This is the Data Template Selector. Which selects the DataTemplate depending upon the data inside the presenter.

Data Template Selector Class.

public class TaskListDataTemplateSelector : DataTemplateSelector

{

public override DataTemplate SelectTemplate(object item, DependencyObject container)

{

ContentPresenter presenter = (ContentPresenter)container;

if (presenter.Content.ToString().Contains(".doc"))

return (DataTemplate)presenter.FindResource("WordDocDataTemplate");

else if (presenter.Content.ToString().Contains(".txt"))

return (DataTemplate)presenter.FindResource("TextFileDataTemplate");

else if (presenter.Content.ToString().Contains(".ppt"))

return (DataTemplate)presenter.FindResource("PowerPointDataTemplate");

else

return (DataTemplate)presenter.FindResource("FolderDataTemplate");

}

}

public class TaskTypeDataTemplateSelector : DataTemplateSelector

{

public override DataTemplate SelectTemplate(object item, DependencyObject container)

{

ContentPresenter presenter = (ContentPresenter)container;

if (presenter.Content.ToString().Contains(".doc"))

return (DataTemplate)presenter.FindResource("WordDocTypeDataTemplate");

else if (presenter.Content.ToString().Contains(".txt"))

return (DataTemplate)presenter.FindResource("TextFileTypeDataTemplate");

else if (presenter.Content.ToString().Contains(".ppt"))

return (DataTemplate)presenter.FindResource("PowerPointTypeDataTemplate");

else

return (DataTemplate)presenter.FindResource("FolderTypeDataTemplate");

}

}

This is how i bound the ListView using the CelTemplateSelector.

List View which uses the ItemSource with the Datatemplate Selector...

<ListView x:Name="lstFileList" Width="460" Margin="3,0,0,0" Height="196" FontFamily="Arial" FontSize="10"

ItemsSource="{Binding ElementName=OpenDialogControl, Path=STRFileList, Mode=OneWay}"

SelectionMode="Extended">

<ListView.View>

<GridView>

<GridViewColumn Header="Name" Width="200"

CellTemplateSelector="{StaticResource myDataTemplateSelector}"/>

<GridViewColumn Header="Type"

CellTemplateSelector="{StaticResource myTypeTemplateSelector}"/>

<GridViewColumn Header="Size"

CellTemplate="{StaticResource SizeDataTemplate}"/>

</< FONT>GridView>

</< FONT>ListView.View>

</< FONT>ListView>

This is the xaml.cs file which defines the STRFileList object. I fill data into this ObservableCollection during the Page_Load event.

Xaml.cs file

private ObservableCollection<ListViewItem> _strFileList = new System.Collections.ObjectModel.ObservableCollection<ListViewItem>();

public ObservableCollection<ListViewItem> STRFileList

{

get { return _strFileList; }

set { _strFileList = value; }

}

Hope this helps...

~Ram






Re: Windows Presentation Foundation (WPF) Wpf DataTemplate Question

PravinK

Is "CellTemplateSelector" works for every row In my case "SelectTemplate" is not getting called for every row.
I have similar code.

Thanks,





Re: Windows Presentation Foundation (WPF) Wpf DataTemplate Question

Ramanujam Sampath

Hi Pravin,

yes CellTemplateSelector is called in every row. As each of my row has different image depending upon the extension of the file.

Hope this helps.

~Ram






Re: Windows Presentation Foundation (WPF) Wpf DataTemplate Question

PravinK

Thanks a lot Ram,

The problem is resolved, but I have one more question.

When the source gets updated then my custom DataTemplate selector's SelectTemplate method should be called again

Am I expecting the right behavior or not Because its not getting called when I change the datasource.

Thanks,