WPFでカスタムコントロールを作成しています。私はList<IMyInterface>
をdependency property
にバインドします。これは次に、すべての項目を期待どおりに表示するListBox
に再度バインドされます。ObservableCollectionの場合、リストをテキストボックスにバインドできません。
このリストの1つのアイテムをTextblock
にバインドしたいので、リスト全体をtextblock
にバインドします。私はこの中にconverter
を持っていて、私が望む単一のアイテムを抽出します。
は、それがうまく働いていますが、いくつかの理由のために、私は、実行時に私のObservabaleCollection
で値を変更すると、奇妙なことに代わりList
のObservableCollection
を使用したい、値がListBox
(成功)に示されています私のtextblock
ではそうではありません。 converter
はヒットしません!
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
this.Errors = new ObservableCollection<IEventDetail>();
this.Errors.CollectionChanged += Errors_CollectionChanged;
var bw = new BackgroundWorker();
bw.DoWork += ((o, e) =>
{
System.Threading.Thread.Sleep(1500);
Dispatcher.Invoke(() =>
{
this.Errors.Add(new MyEvents("example of some detail", "Failed title"));
});
System.Threading.Thread.Sleep(2500);
Dispatcher.Invoke(() =>
{
this.Errors.Add(new MyEvents("Another example", "Failed title 2"));
});
});
bw.RunWorkerAsync();//background worker for testing/debugging only
}
private void Errors_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
OnPropertyChanged("Errors");
}
private ObservableCollection<IEventDetail> _errors;
public ObservableCollection<IEventDetail> Errors
{
get
{
return this._errors;
}
set
{
this._errors = value;
OnPropertyChanged("Errors");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged == null)
return;
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
とXAMLは、単にあなたが見ることができるように、私はその後、火INotifyPropertyChanged
を強制的にCollectionChanged
イベントを使用しようとした
<local:Notify Events="{Binding Errors}" DockPanel.Dock="Right"/>
ですが、それはConverter
はまだListBox
が私の中に発射です罰金を更新
これはUserControls
XAML
私は別のことをする必要がありますか?コメントで述べたように
問題を必要とせず、代わりに
TextBlock.Text
Events.ConvertedText
にプロパティをバインドを何だろうカスタムプロパティでObservableCollection<T>
から継承された独自のコレクションクラスを作成します。 xamlになければならず、onpropertychangedはobservableCollectionに暗黙的に指定されています。 (あなたはxamlを投稿していますか?)explicti双方向バインディングを設定しようとしています。UpdataSourceTrigger = PropertyChanged – Xilmiki@ Xilmiki、ありがとうございますが、バインディング**は** ListBox'で動作しますが、 'Textblock'では動作しません。だから私は2ウェイバインディングが必要かどうか分からない、または 'UpdateSourceTrigger'を使うこともありません。 – Dave
明示的な双方向バインディングを設定しようとしていて、UpdataSourceTrigger = PropertyChanged = TextBoxバインディングでWPFプロジェクトで同じ問題がありました。 – Xilmiki