Articles → WPF → Re Template Export File Combo Box Using Data Template

Re Template Export File Combo Box Using Data Template





Picture showing how the dropdown will look like post retemplate in wpf
Click to Enlarge





public class ExportOption {
  public string ExportCode {
    get;
    set;
  }

  public string ExportDisplayText {
    get;
    set;
  }
}




public List < ExportOption > ExportCollection {
  get;
  set;
}






ExportCollection = new List<ExportOption>();

ExportOption option = new ExportOption();
option.ExportCode = "WORD";
option.ExportDisplayText = "Export to Word";
ExportCollection.Add(option);

option = new ExportOption();
option.ExportCode = "EXCEL";
option.ExportDisplayText = "Export to Excel";
ExportCollection.Add(option);

option = new ExportOption();
option.ExportCode = "PDF";
option.ExportDisplayText = "Export to PDF";
ExportCollection.Add(option);

ExportComboBox.ItemsSource = ExportCollection;






<ComboBox x:Name="ExportComboBox" DataContext="{Binding}" Width="200" Height="25"                           
   SelectedValuePath="ExportCode" ItemTemplate="{StaticResource MyTemplate}">




<DataTemplate x:Key="MyTemplate">
	<Grid>
		<Grid.ColumnDefinitions>
			<ColumnDefinition Width="50"/>
			<ColumnDefinition Width="Auto"/>
		</Grid.ColumnDefinitions>
		<Image x:Name="MyImage" Width="14" Height="15"></Image>
		<TextBlock x:Name="MyTextblock" Grid.Column="1" Text="{Binding ExportDisplayText}"></TextBlock>
	</Grid>
	<DataTemplate.Triggers>
		<DataTrigger Binding="{Binding ExportCode}" Value="WORD">
			<Setter Property="Source" Value="Images/Word.png" TargetName="MyImage"></Setter>
		</DataTrigger>
		<DataTrigger Binding="{Binding ExportCode}" Value="EXCEL">
			<Setter Property="Source" Value="Images\Excel.png" TargetName="MyImage"></Setter>
		</DataTrigger>
		<DataTrigger Binding="{Binding ExportCode}" Value="PDF">
			<Setter Property="Source" Value="Images\PDF.png" TargetName="MyImage"></Setter>
		</DataTrigger>
	</DataTemplate.Triggers>
</DataTemplate>








Picture showing changing the build action of an image to Resource
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, April 5, 2011
 
Updated On  -  Wednesday, August 10, 2022

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250