2009-07-13 7 views
1

私はSharpComboBoxというUserControlを持っています。 MVVMモデルを使用して、SharpComboBoxにカテゴリを設定しています。そのためにItemsSourceプロパティを設定する必要があります。 SharpComboBoxコントロールの使い方を以下に示します。依存プロパティWPF常にNULLを返す

<sharpControls:SharpComboBox ItemsSource="{Binding Path=Categories}" Grid.Column="1" Grid.Row="1" DisplayMemberPath="Title"> 

      </sharpControls:SharpComboBox> 

ウィンドウがAddBook.xamlと呼ばれ、ここでの背後にあるコードです:

public AddBooks() 
     { 
      InitializeComponent(); 

      this.DataContext = new AddBookViewModel(); 

     } 

そして、ここではAddBookViewModelの実装です。ここ

public class AddBookViewModel 
    { 

     private CategoryRepository _categoryRepository; 

     public AddBookViewModel() 
     { 
      _categoryRepository = new CategoryRepository(); 
     } 



     public List<Category> Categories 
     { 
      get 
      { 
       return _categoryRepository.GetAll(); 
      } 
     } 

そして最後にSharpComboBoxコントロールです:ItemsSourceプロパティは常にnullであるいくつかの理由

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) 
     { 

     } 

<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> 

そしてここでは、背後にあるコードです。

更新日:

あなたは、単にコンストラクタで一度あなたの財産からcomboBox.ItemsSourceを設定することはできません
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); 
     } 
+0

コードビューからコントロールを参照して強制的に更新する必要がある場合は、コードムールが正しくないか、INotifyPropertyChangedを使用して更新する必要があるバインディングを通知しない場合があります。 –

+0

また、MVVMを使用してアプリケーションを設計しようとするときに、目標である0コードビハインドを見ることをお勧めします。それは完全に可能です。 –

答えて

0

は、誰がそれが起こるか早く知っています。これらの2つのプロパティ間にバインディングを設定する必要があります。

+0

これを反映するためにコードを編集したり編集したりできますか? – azamsharp

+0

私はコードを更新しましたが、独自の依存関係プロパティであるItemsSourceは常にnullを返します。 – azamsharp

+0

コードが更新されました! – azamsharp

2

kek444は非常に近いですが、欠落している重要な要素が1つあります。あなたのViewModelがINotifyPropertyChangedを実装していないことに気付きました。これにより、そのプロパティを設定したときにバインドが自動的にリフレッシュされなくなります。したがって、kek444が述べたように、あなたはnullにバインドされています(早いので)、設定したときにViewに変更を通知していません。しかし、変更するのはかなり簡単です。

public class AddBookViewModel : INotifyPropertyChanged 
{ 
    event PropertyChangedEventHandler PropertyChanged; 
    public AddBookViewModel() 
    { 
     _categoryRepository = new CategoryRepository(); 
     if(PropertyChanged != null) 
     { 
       PropertyChanged(this, new PropertyChangedEventArgs("Categories"); 
     } 
    } 
    ... 
} 

バッキングストア(CategoryRepository)を変更する場合は、いつでもこの操作を行います。あなたのリポジトリが実装されているかどうかに応じていくつかの追加の合併症があるかもしれませんが、この情報は少なくとも何が起こっているのかを説明しなければなりません。

私は一般的に、INotifyPropertyChangedを実装する基本ViewModelクラスを作成します。そのため、PropertyChangedロジックをラップするいくつかのヘルパーメソッドを追加できます... OnPropertyChanged( "MyProp");以上です。このことができますDebugging WPF Binding

希望を:あなたを助けるかもしれない

もうひとつは、あなたがそれらを正しく設定した場合、バインディングは、デバッグ出力に報告することです。

関連する問題