1

我想在 XAML 中绑定 BillboardTextGroupVisual3D 中的项目,并且我还检查了有关项目的 DependencyProperty 的源代码,如下所示

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

在这里,我通过了我尝试过的代码段,但所有这些都失败了。我不知道如何动态更新列表中的 DependencyProperty。

在 MainWindows.xaml 中,我将 Items 与 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++;
    }
}
4

0 回答 0