XAML で BillboardTextGroupVisual3D のアイテムをバインドしたいのですが、次のようにアイテムの DependencyProperty に関するソース コードも確認します。
public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register(
"Items",
typeof(IList<BillboardTextItem>),
typeof(BillboardTextGroupVisual3D),
new UIPropertyMetadata(null, VisualChanged));
ここで、私が試したスニペット コードを過ぎましたが、それらはすべて失敗しました。List の DependencyProperty を動的に更新する方法がわかりません。
MainWindows.xaml で、項目を TextItems3 にバインドします。
<h:BillboardTextGroupVisual3D Background="White" BorderBrush="Black" Foreground="Black" BorderThickness="1" FontSize="12" Padding="2"
Offset="20,20" PinBrush="Gray" Items="{Binding TextItems3}" IsEnabled="True" />
MainWindows.xaml.cs で、ObservableCollection を使用するように言われましたが、それでも失敗します..
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public IList<BillboardTextItem> textItems3;
public IList<BillboardTextItem> TextItems3
{
get
{
return textItems3;
}
set
{
textItems3 = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("TextItems3"));
}
}
}
DispatcherTimer _timer;
public MainWindow()
{
InitializeComponent();
this.textItems3 = new ObservableCollection<BillboardTextItem>();
this.textItems3.Add(new BillboardTextItem { Text = "It's be added in the first executed.", Position = new Point3D(i * 10, 0, 0.5), DepthOffset = 0, WorldDepthOffset = 0.2 });
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(1000);
//加入callback function
_timer.Tick += _timer_Tick;
//開始
_timer.Start();
this.DataContext = this;
}
int i = 0;
void _timer_Tick(object sender, EventArgs e)
{
this.textItems3.Add(new BillboardTextItem { Text = "I want to add this dynamically.", Position = new Point3D(i*10, 0, 0.5), DepthOffset = 0, WorldDepthOffset = 0.2 });
TextItems3 = textItems3;
i++;
}
}