ComboBox et SelectionBoxItemTemplate
Dans une combobox, j'ai défini un ItemTemplate, qui est constitué de 2 TextBlock dans un StackPanel. Quand je déroule la ComboBox, les deux TextBlock s'affichent très bien.
Je souhaite, quand la ComboBox est "au repos", afficher dans le cadre uniquement un des deux TextBlock. J'ai pensé au SelectionBoxItemTemplate, mais il est malheureusement en Get uniquement (et en Set private).
Comment puis-je outrepasser ça ? J'ai pensé faire un trigger pour modifier la Visibility d'un des TextBlock quand l'élément est l'élément sélectionné, sans succès.
Si quelqu'un est parvenu à ce que je recherche, la réponse m'intéresse beaucoup.
Le code d'exemple de ce que je recherche
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
<Window x:Class="TestComboBox.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="mySelectionBoxItemTemplate">
<TextBlock Text="{Binding Path=Nom}" />
</DataTemplate>
<DataTemplate x:Key="myComboTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Nom}" />
<TextBlock Text=" - " />
<TextBlock Text="{Binding Path=Prenom}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ComboBox ItemsSource="{StaticResource People}" ItemTemplate="{StaticResource myComboTemplate}"
SelectionBoxItemTemplate="{StaticResource mySelectionBoxItemTemplate}"> <!-- Ca ne fonctionne pas à cause du Set private -->
</ComboBox>
</Grid>
</Window> |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TestComboBox
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
List<Personne> people = new List<Personne>();
people.Add(new Personne("Moulin", "Jean"));
people.Add(new Personne("Bartowksi", "Chuck"));
people.Add(new Personne("Mulder", "Fox"));
people.Add(new Personne("Hughes", "Howard"));
people.Add(new Personne("Alesi", "Jean"));
this.Resources["People"] = people;
InitializeComponent();
}
}
public class Personne : INotifyPropertyChanged
{
string sNom;
string sPrenom;
public Personne(string nom, string prenom)
{
sNom = nom;
sPrenom = prenom;
}
public string Nom
{
get { return sNom; }
set { sNom = value; OnPropertyChanged("Nom"); }
}
public string Prenom
{
get { return sPrenom; }
set { sPrenom = value; OnPropertyChanged("Prenom"); }
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged == null) return;
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
} |