1

SharpComboBox という UserControl があります。SharpComboBox にカテゴリを設定するために MVVM モデルを使用しています。そのためには、ItemsSource プロパティを設定する必要があります。これが SharpComboBox コントロールの使用法です。

  <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 コントロールを次に示します。

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

コードビハインドは次のとおりです。

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

        }

何らかの理由で、ItemsSource プロパティは常に null です。

更新しました:

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); 
        } 
4

2 に答える 2

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"); を呼び出すだけです。以上です。

あなたを助けるかもしれないもう1つのことは、バインディングを正しく構成すると、バインディングがデバッグ出力に報告されることです: WPFバインディングのデバッグ

お役に立てれば。

于 2009-07-13T19:33:04.760 に答える