私はBlackoutDatesプロパティに日付のリストをバインドしたいと思いますが、実際には可能ではないようです。特にMVVMのシナリオでは。誰もこれのような何かを達成しましたか? MVVMでうまくいく優れたカレンダーコントロールはありますか?WPF ToolkitのカレンダーコントロールでBlackoutDateをバインドする方法は?
答えて
はあなたの日付ピッカーのジレンマのために、私は(CommandBindingsの私の使用から変更)添付プロパティを使用してきちんとしたハックを見つけました:
class AttachedProperties : DependencyObject
{
#region RegisterBlackoutDates
// Adds a collection of command bindings to a date picker's existing BlackoutDates collection, since the collections are immutable and can't be bound to otherwise.
//
// Usage: <DatePicker hacks:AttachedProperties.RegisterBlackoutDates="{Binding BlackoutDates}" >
public static DependencyProperty RegisterBlackoutDatesProperty = DependencyProperty.RegisterAttached("RegisterBlackoutDates", typeof(System.Windows.Controls.CalendarBlackoutDatesCollection), typeof(AttachedProperties), new PropertyMetadata(null, OnRegisterCommandBindingChanged));
public static void SetRegisterBlackoutDates(UIElement element, System.Windows.Controls.CalendarBlackoutDatesCollection value)
{
if (element != null)
element.SetValue(RegisterBlackoutDatesProperty, value);
}
public static System.Windows.Controls.CalendarBlackoutDatesCollection GetRegisterBlackoutDates(UIElement element)
{
return (element != null ? (System.Windows.Controls.CalendarBlackoutDatesCollection)element.GetValue(RegisterBlackoutDatesProperty) : null);
}
private static void OnRegisterCommandBindingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
System.Windows.Controls.DatePicker element = sender as System.Windows.Controls.DatePicker;
if (element != null)
{
System.Windows.Controls.CalendarBlackoutDatesCollection bindings = e.NewValue as System.Windows.Controls.CalendarBlackoutDatesCollection;
if (bindings != null)
{
element.BlackoutDates.Clear();
foreach (var dateRange in bindings)
{
element.BlackoutDates.Add(dateRange);
}
}
}
}
#endregion
}
私は、私はあなたを助けるためには遅すぎるんだと確信しているが、うまくいけば誰かそうでなければ、それは役に立つと思うでしょう。
当分の間、そのプロジェクトは保留になっていません。私はまだAttached Propertiesを理解しているかどうかはわかりませんが、それを行う方法だと思っていました。 WPFのバンドワゴンに戻る時間。 – nportelli
ええ、私は100%ではありませんが、添付プロパティはまだありますが、2つの用途があるようです:1. Canvasオブジェクトのような追加データ用のDynamic-Object-keyed辞書の代わり、または2 XAMLの拡張メソッドのような使い方。うーん、主題のコミュニティwikiは本当に興味深いかもしれません。 –
私は上記の例(AttachedPropertiesクラス)を実装しました。
public CalendarBlackoutDatesCollection BlackoutDates
{
get
{
return _blackoutDates;
}
set
{
_blackoutDates = value;
this.RaisePropertyChanged(p => p.BlackoutDates);
}
}
ObservableBaseからこれのViewModel inerits:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Data;
using System.Collections;
namespace MySolution
{
public abstract class ObservableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
これは、このプロパティを使用してウィンドウ内にXAMLです:
今<Window x:Class="MySolution.MainWindow"
xmlns:local="clr-namespace:MySolution">
<Grid>
<DatePicker x:Name="datePicker" Grid.Row="0" Height="30"
local:AttachedProperties.RegisterBlackoutDates="{Binding BlackoutDates}">
</DatePicker>
</Grid>
私はこのように私のViewModelでプロパティを作成しましたBlackoutDateをカレンダーに追加する場合は、私のViewModelでUpdateCalendarBlackoutDatesを呼び出します。
private void UpdateCalendarBlackoutDates()
{
CalendarDateRange r = new CalendarDateRange(new DateTime(2010, 12, 9), new DateTime(2010, 12, 9));
CalendarDateRange r2 = new CalendarDateRange(new DateTime(2010, 12, 10), new DateTime(2010, 12, 10));
// Because we can't reach the real calendar from the viewmodel, and we can't create a
// new CalendarBlackoutDatesCollection without specifying a Calendar to
// the constructor, we provide a "Dummy calendar", only to satisfy
// the CalendarBlackoutDatesCollection...
// because you can't do: BlackoutDates = new CalendarBlackoutDatesCollection().
Calendar dummyCal = new Calendar();
BlackoutDates = new CalendarBlackoutDatesCollection(dummyCal);
// Add the dateranges to the BlackOutDates property
BlackoutDates.Add(r);
BlackoutDates.Add(r2);
}
これは私にとって完璧に機能します。それは、さらに代わりCalendarBlackoutDatesCollectionのDateRangesの一覧を受け入れるようにOnRegisterCommandBindingChanged方法を変更し、このようなリストにプロパティを変更することで完成することができます。..
public List<CalendarDateRange> BlackoutDates
{
etc.
が、今、これは私の作品のための
これは私のために働いた、ありがとう。 – dev1998
ここでは、BlackoutDateを通常のObservableコレクションと同じように扱うことができる改良されたMattの答えを示します(BlackoutDateを変更するたびに新しいコレクションを作成する必要はありません)。バインドされたすべてのカレンダーと日付ピッカーのリストを格納し、タグ内にMVVMで使用されているコレクションを格納します。クラスの簡単な変更が必要な場合のObservableCollection <のDateTime >と連携できるようになります:
// Adds a collection of command bindings to a date picker's existing BlackoutDates collection, since the collections are immutable and can't be bound to otherwise.
// Usage: <DatePicker hacks:AttachedProperties.RegisterBlackoutDates="{Binding BlackoutDates}" >
public class CalendarAttachedProperties : DependencyObject
{
#region Attributes
private static readonly List<Calendar> _calendars = new List<Calendar>();
private static readonly List<DatePicker> _datePickers = new List<DatePicker>();
#endregion
#region Dependency Properties
public static DependencyProperty RegisterBlackoutDatesProperty = DependencyProperty.RegisterAttached("RegisterBlackoutDates", typeof(ObservableCollection<DateTime>), typeof(CalendarAttachedProperties), new PropertyMetadata(null, OnRegisterCommandBindingChanged));
public static void SetRegisterBlackoutDates(DependencyObject d, ObservableCollection<DateTime> value)
{
d.SetValue(RegisterBlackoutDatesProperty, value);
}
public static ObservableCollection<DateTime> GetRegisterBlackoutDates(DependencyObject d)
{
return (ObservableCollection<DateTime>)d.GetValue(RegisterBlackoutDatesProperty);
}
#endregion
#region Event Handlers
private static void CalendarBindings_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
ObservableCollection<DateTime> blackoutDates = sender as ObservableCollection<DateTime>;
Calendar calendar = _calendars.First(c => c.Tag == blackoutDates);
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (DateTime date in e.NewItems)
{
calendar.BlackoutDates.Add(new CalendarDateRange(date));
}
}
}
private static void DatePickerBindings_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
ObservableCollection<DateTime> blackoutDates = sender as ObservableCollection<DateTime>;
DatePicker datePicker = _datePickers.First(c => c.Tag == blackoutDates);
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (DateTime date in e.NewItems)
{
datePicker.BlackoutDates.Add(new CalendarDateRange(date));
}
}
}
#endregion
#region Private Methods
private static void OnRegisterCommandBindingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
Calendar calendar = sender as Calendar;
if (calendar != null)
{
ObservableCollection<DateTime> bindings = e.NewValue as ObservableCollection<DateTime>;
if (bindings != null)
{
if (!_calendars.Contains(calendar))
{
calendar.Tag = bindings;
_calendars.Add(calendar);
}
calendar.BlackoutDates.Clear();
foreach (DateTime date in bindings)
{
calendar.BlackoutDates.Add(new CalendarDateRange(date));
}
bindings.CollectionChanged += CalendarBindings_CollectionChanged;
}
}
else
{
DatePicker datePicker = sender as DatePicker;
if (datePicker != null)
{
ObservableCollection<DateTime> bindings = e.NewValue as ObservableCollection<DateTime>;
if (bindings != null)
{
if (!_datePickers.Contains(datePicker))
{
datePicker.Tag = bindings;
_datePickers.Add(datePicker);
}
datePicker.BlackoutDates.Clear();
foreach (DateTime date in bindings)
{
datePicker.BlackoutDates.Add(new CalendarDateRange(date));
}
bindings.CollectionChanged += DatePickerBindings_CollectionChanged;
}
}
}
}
#endregion
}
- 1. Xtended WPF Toolkit WizardのCurrentPageをMVVMにバインドする方法は?
- 2. WPF Toolkitカレンダーコントロール - 翌月のスクロールを防ぐ方法を教えてください。
- 3. WPF Toolkit - LineSeriesをコードの背後にバインド
- 4. WPF Toolkitの線グラフで軸を入れ替える方法は?
- 5. WPFリストボックス:データをバインドする方法は?
- 6. WPFでコマンドをバインドする方法
- 7. WPF Toolkit ColumnSeriesチャートをリアルタイムでリフレッシュする方法
- 8. リストボックスデータテンプレートのリストビューをバインドする方法WPF
- 9. WPF Toolkit PropertyGridスタイル
- 10. WPF Toolkit Datagrid - カスタムタブ
- 11. WPF ToolKit DataGridパフォーマンス
- 12. WPF Toolkit BusyIndicator
- 13. WPF Toolkit DataGrid Multi-Select:SelectedItemsを取得する方法?
- 14. asp.netのカレンダーコントロールを検証する方法
- 15. WPF Chart Toolkitのドキュメント
- 16. コンボボックスをツールチップwpfにバインドする方法
- 17. WPFで関連するテーブルをバインドする方法は?
- 18. WPFで2つのDataGridColumns幅をバインドする方法は?
- 19. コード内のWPF DataGridでデータをバインドする方法は?
- 20. WPFコードでGridViewの列幅をバインドする方法は?
- 21. WPFで逆ブール値のプロパティをバインドする方法は?
- 22. WPFでは、プロパティの一部にバインドする方法は?
- 23. Extended WPF ToolkitのColorPicker.SelectedColorChangedイベントを現在のRichTextBoxに送信する方法は?
- 24. WPF Toolkitデータグリッドが16K行のDataTableにバインドされるとクラッシュする
- 25. WPF DataGrid:カスタムプロパティにバインドする方法
- 26. WPF Toolkitをリセットする方法データをリセットするときのチャートのカラーパレット
- 27. WPF - XAMLで汎用メソッドをバインドする方法は?
- 28. 拡張WPF Toolkitを使用
- 29. WPFをサポートするFinancial Charting Toolkit?
- 30. WPFのMainWindowクラスからICommandをバインドする方法は?
あなたはBlackoutDatesにバインドしようとすると何が起こる:
ここでのObservableCollection <のDateTime >バージョンはありますか?エラーが発生していますか? – user200783
Blackoutdatesはオプションでもありません...私はDatepickerを使用していると思いますが、カレンダーとテキストボックスを使用したと思っています。 – nportelli