1

というユーザーコントロール内に次のものがありますUserInputOutput

<ComboBox Grid.Column="1" Background="White" Visibility="{Binding InputEnumVisibility}"     
          FontSize="{Binding FontSizeValue}" Width="Auto" Padding="10,0,5,0"     
          ItemsSource="{Binding EnumItems}"     
          SelectedIndex="{Binding EnumSelectedIndex}"/>    

ここにはいくつかのバインディングがあり、ItemsSource を除いてすべてうまく機能します。これが私の依存関係プロパティとパブリック変数です。

public ObservableCollection<String> EnumItems
{
    get { return (ObservableCollection<String>)GetValue(EnumItemsProperty); }
    set { SetValue(EnumItemsProperty, value); }
}

public static readonly DependencyProperty EnumItemsProperty =
    DependencyProperty.Register("EnumItems", typeof(ObservableCollection<string>),typeof(UserInputOutput)

ComboBox の ItemSource を除いて、すべてのバインディングは XAML で設定されます。これは実行時に設定する必要があります。私のコードでは、次を使用します。

ObservableCollection<string> enumItems = new ObservableCollection<string>();
UserInputOutput.getEnumItems(enumItems, enumSelectedIndex, ui.ID, ui.SubmodeID);
instanceOfUserInputOutput.EnumItems = enumItems;

XAML がファイルから読み込まれた後に、このコードを実行します。enumItemsinstaceOfUserInputOutput.EnumItemsに設定した後、正しい項目が含まれていますが、プログラムのコンボ ボックスに表示されません。

ここでどこが間違っているのかわかりません。何かご意見は?

ありがとうございました!

4

1 に答える 1

0

I assume your ViewModel class (the one that is used as a source of binding) implements INotifyPropertyChanged interface. Otherwise update won't work.

Then, in your setter method, do this:

set
{
     // set whatever internal properties you like
     ...

     // signal to bound view object which properties need to be refreshed
     OnPropertyChanged("EnumItems");
}

where OnProperyChanged method is like this:

protected void OnPropertyChanged(string propertyName)
{
    PropertyChangedEventHandler handler = this.PropertyChanged;
    if (handler != null)
    {
        var e = new PropertyChangedEventArgs(propertyName);
        handler(this, e);
    }
}

BTW, I don't know why you need to declare EnumItems as a dependency property. Having it as a class field would work fine, unless you want to use it as a target for binding (right now it is used as a binding source).

于 2012-08-29T20:15:45.353 に答える