J'ai un UserControl appelé SharpComboBox. J'utilise le modèle MVVM pour remplir le SharpComboBox avec des catégories. Pour cela, j'ai besoin de définir la propriété ItemsSource. Voici l'utilisation du contrôle SharpComboBox.Propriété de dépendance WPF retournant toujours NULL
<sharpControls:SharpComboBox ItemsSource="{Binding Path=Categories}" Grid.Column="1" Grid.Row="1" DisplayMemberPath="Title">
</sharpControls:SharpComboBox>
La fenêtre est appelée AddBook.xaml et voici le code derrière:
public AddBooks()
{
InitializeComponent();
this.DataContext = new AddBookViewModel();
}
Et voici la mise en œuvre du AddBookViewModel.
public class AddBookViewModel
{
private CategoryRepository _categoryRepository;
public AddBookViewModel()
{
_categoryRepository = new CategoryRepository();
}
public List<Category> Categories
{
get
{
return _categoryRepository.GetAll();
}
}
Et enfin voici le contrôle de SharpComboBox:
<StackPanel Name="stackPanel">
<ComboBox x:Name="comboBox">
<ComboBox.ItemTemplate>
<DataTemplate>
<ItemsControl>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="2*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
</Grid>
<TextBlock Grid.Column="0" Grid.Row="0" Text="{Binding Path=Title}" Margin="10" />
<Image Grid.Column="1" Margin="10" Grid.Row="0" Width="100" Height="100" Stretch="Fill" Source="{Binding Path=ImageUrl}">
</Image>
</ItemsControl>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
Et voici le code derrière:
public partial class SharpComboBox : UserControl
{
public static DependencyProperty ItemsSourceProperty;
public SharpComboBox()
{
InitializeComponent();
this.DataContextChanged += new System.Windows.DependencyPropertyChangedEventHandler(SharpComboBox_DataContextChanged);
ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof (IEnumerable),
typeof (SharpComboBox), null);
comboBox.ItemsSource = ItemsSource;
}
public IEnumerable ItemsSource
{
get { return (IEnumerable) GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
void SharpComboBox_DataContextChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
{
}
Pour une raison quelconque la propriété ItemsSource est toujours nulle.
mise à jour:
void SharpComboBox_DataContextChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
{
var binding = new Binding();
binding.Source = this.DataContext;
**binding.Path = new PropertyPath("Categories");**
comboBox.SetBinding(ComboBox.ItemsSourceProperty, binding);
}
Si vous devez référencer des contrôles sur votre View à partir de votre codebehind pour les forcer à s'actualiser, codemell pour votre liaison n'est pas correct ou vous ne notifiez pas la liaison qu'il doit être mis à jour en utilisant INotifyPropertyChanged. –
Aussi, je vous encourage à regarder 0 codebehind étant votre objectif lorsque vous essayez de concevoir votre application avec MVVM. C'est entièrement possible. –