This project is read-only.

List<string> Datatemplate hack

Nov 30, 2010 at 8:07 PM

Not very elegant, new to wpf, but here is how I added in a List<string> data template. I couldnt do it in pure xaml, however i suspect there is a solution in pure xaml (suggestions?). So, this solution involves adding a code-behind to WPGTemplate.xaml.

Changes:

WPGTemplate.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                    xmlns:clr="clr-namespace:System;assembly=mscorlib" 
                    xmlns:clr2="clr-namespace:System.Collections;assembly=mscorlib"
                    xmlns:generics="clr-namespace:System.Collections.Generic;assembly=mscorlib" //added
                    xmlns:local="clr-namespace:WPG" 
		  xmlns:data="clr-namespace:WPG.Data" 
                    xmlns:converters="clr-namespace:WPG.Converters" 
                    xmlns:typeeditors="clr-namespace:WPG.TypeEditors"
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2006" 
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                    x:Class="WPG.Themes.ExpressionBlend.WPGTemplates" //added
                    x:ClassModifier="public"> //added
 
 
add a new datatemplate:
    <DataTemplate x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:PropertyGrid}, ResourceId={x:Type generics:List`1[System.String]}}">
        <ComboBox Style="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type local:PropertyGrid}, ResourceId=ComboBoxStyle}}" x:Name="comboBox" SelectionChanged="cbListString_SelectionChanged" Loaded="comboBox_Loaded" ItemsSource="{Binding Path=Value, Mode=TwoWay}"/>
    </DataTemplate>

 WPGTemplate.xaml.cs (new file) -- used to move the selected item to the top of the list. Again, there is probably a xaml way to do this, but I am not sure of the solution.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;

namespace WPG.Themes.ExpressionBlend
{
   public partial class WPGTemplates
   {
      private void cbListString_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
         ComboBox box = (sender as ComboBox);
         List<string> stringList = box.ItemsSource as List<string>;
         if (stringList != null)
         {
            if (e.AddedItems.Count > 0)
            {
               string stringValue = e.AddedItems[0] as string;
               if (stringValue != null)
               {
                  stringList.Remove(stringValue);
                  stringList.Insert(0, stringValue);
                  box.ItemsSource = stringList;
               }
            }
         }
      }

      private void comboBox_Loaded(object sender, System.Windows.RoutedEventArgs e)
      {
         ComboBox box = sender as ComboBox;
         box.SelectedItem = (box.ItemsSource as List<string>).First();
      }
   }
}