1

I want to bind items in BillboardTextGroupVisual3D in the XAML and I also check the source code about the Items's DependencyProperty as follows

public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register(
"Items",
typeof(IList<BillboardTextItem>),
typeof(BillboardTextGroupVisual3D),
new UIPropertyMetadata(null, VisualChanged));

Here I past the snippet code I have been tried, but all of those were fail. I have no idea how to dynamic update the DependencyProperty in List.

In the MainWindows.xaml, I bind the Items with 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"  />

In MainWindows.xaml.cs, I was talked to use the ObservableCollection, but still fail..

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++;
    }
}
wkl
  • 1,896
  • 2
  • 15
  • 26
Sheng-Fang
  • 21
  • 4

0 Answers0