Click here to Skip to main content
15,886,873 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: Listbox items - how to "owner draw" Pin
#realJSOP23-Mar-09 23:21
mve#realJSOP23-Mar-09 23:21 
AnswerRe: Listbox items - how to "owner draw" Pin
ABitSmart23-Mar-09 17:28
ABitSmart23-Mar-09 17:28 
GeneralRe: Listbox items - how to "owner draw" Pin
#realJSOP23-Mar-09 23:20
mve#realJSOP23-Mar-09 23:20 
AnswerRe: Listbox items - how to "owner draw" Pin
Pete O'Hanlon24-Mar-09 1:30
mvePete O'Hanlon24-Mar-09 1:30 
GeneralRe: Listbox items - how to "owner draw" Pin
#realJSOP24-Mar-09 2:55
mve#realJSOP24-Mar-09 2:55 
GeneralRe: Listbox items - how to "owner draw" Pin
Pete O'Hanlon24-Mar-09 3:24
mvePete O'Hanlon24-Mar-09 3:24 
GeneralRe: Listbox items - how to "owner draw" Pin
#realJSOP24-Mar-09 3:56
mve#realJSOP24-Mar-09 3:56 
GeneralRe: Listbox items - how to "owner draw" Pin
Pete O'Hanlon24-Mar-09 5:37
mvePete O'Hanlon24-Mar-09 5:37 
John - here's a simple application that you can use to test this type of functionality. I've added an ObservableCollection which consists of words that the user adds in via a textbox.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows;

namespace SampleWpfApplication
{
  public class WordManager
  {
    public string Text { get; set; }
  }

  public class Words : ObservableCollection<WordManager>
  {
  }

  public class WordTemplateSelector : DataTemplateSelector
  {
    public DataTemplate NormalWordTemplate { get; set; }
    public DataTemplate RedWordTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
      WordManager word = item as WordManager;
      if (word.Text == "Howdy")
      {
        return RedWordTemplate;
      }
      else
      {
        return NormalWordTemplate;
      }
    }
  }
}
Here's the XAML that's used:
<Window x:Class="SampleWpfApplication.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SampleWpfApplication"
  Title="Window1" Height="300" Width="300">
  <Window.Resources>
    <DataTemplate x:Key="NormalWords">
      <Grid>
        <TextBlock Text="{Binding Path=Text}" Foreground="Brown"/>
      </Grid>
    </DataTemplate>

    <DataTemplate x:Key="RedWords">
      <TextBlock Text="{Binding Path=Text}" FontWeight="Bold" Foreground="Red"/>
    </DataTemplate>
    
    <local:Words x:Key="Words" />
    <local:WordTemplateSelector 
      x:Key="TemplateSelector" 
      NormalWordTemplate="{StaticResource NormalWords}"
      RedWordTemplate="{StaticResource RedWords}" />
  </Window.Resources>

  <StackPanel>
    <ListBox x:Name="myList" 
         ItemsSource="{Binding Mode=Default, Source={StaticResource Words}}" 
         ItemTemplateSelector="{StaticResource TemplateSelector}"
         x:FieldModifier="private"></ListBox>
    <TextBox x:Name="myText" x:FieldModifier="private" Width="30" />
    <Button Content="Click" Click="Button_Click" />
  </StackPanel>
</Window>
Finally, here's the button click code:
private void Button_Click(object sender, RoutedEventArgs e)
{
  if (!string.IsNullOrEmpty(myText.Text))
  {
    Words words = this.Resources["Words"] as Words;
    if (words != null)
    {
        words.Add(new WordManager{Text=myText.Text});
    }
  }
}
Basically the XAML is set up so that there can be multiple templates, which get applied based on certain conditions - chosen by the WordTemplateSelector class. By providing a template selector class, it's possible to add as many conditions as you like - all of which can have a simple template. The beauty of the approach here, is that we're using an ObservableCollection so that the template gets reevaluated whenever an item is added in. If you need more information, please feel free to ask.

"WPF has many lovers. It's a veritable porn star!" - Josh Smith

As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.


My blog | My articles | MoXAML PowerToys



GeneralRe: Listbox items - how to "owner draw" [modified] Pin
#realJSOP24-Mar-09 10:01
mve#realJSOP24-Mar-09 10:01 
GeneralRe: Listbox items - how to "owner draw" Pin
Pete O'Hanlon24-Mar-09 11:20
mvePete O'Hanlon24-Mar-09 11:20 
GeneralRe: Listbox items - how to "owner draw" Pin
#realJSOP25-Mar-09 3:29
mve#realJSOP25-Mar-09 3:29 
GeneralRe: Listbox items - how to "owner draw" Pin
Pete O'Hanlon25-Mar-09 3:44
mvePete O'Hanlon25-Mar-09 3:44 
GeneralRe: Listbox items - how to "owner draw" Pin
RugbyLeague30-Mar-09 3:51
RugbyLeague30-Mar-09 3:51 
QuestionLoad project into canvas Pin
emptythetill23-Mar-09 10:55
emptythetill23-Mar-09 10:55 
AnswerRe: Load project into canvas Pin
Christian Graus23-Mar-09 15:21
protectorChristian Graus23-Mar-09 15:21 
GeneralRe: Load project into canvas Pin
emptythetill24-Mar-09 7:41
emptythetill24-Mar-09 7:41 
QuestionDrag drop preview Pin
anishkannan23-Mar-09 0:54
anishkannan23-Mar-09 0:54 
AnswerRe: Drag drop preview [modified] Pin
sivaddrahcir23-Mar-09 11:45
sivaddrahcir23-Mar-09 11:45 
QuestionDownloading file from server Pin
sumit703422-Mar-09 23:50
sumit703422-Mar-09 23:50 
AnswerRe: Downloading file from server Pin
Mark Salsbery23-Mar-09 9:51
Mark Salsbery23-Mar-09 9:51 
GeneralRe: Downloading file from server Pin
sumit703423-Mar-09 18:59
sumit703423-Mar-09 18:59 
AnswerRe: Downloading file from server Pin
Braulio Dez1-Apr-09 5:58
Braulio Dez1-Apr-09 5:58 
QuestionRoutedEvent compare to event? Pin
mildred-frontfree22-Mar-09 23:34
mildred-frontfree22-Mar-09 23:34 
AnswerRe: RoutedEvent compare to event? Pin
ABitSmart23-Mar-09 0:04
ABitSmart23-Mar-09 0:04 
GeneralRe: RoutedEvent compare to event? Pin
mildred-frontfree23-Mar-09 0:24
mildred-frontfree23-Mar-09 0:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.