开发者

RelativeSource Binding Tooltip to UserControl DependencyProperty Error

I have a custom usercontrol that does a little animation on a DispatcherTimer tick, as well as update a DependencyProperty of that usercontrol :

public partial class EggCounter : UserControl
{
  DispatcherTimer eggTimer;

  public EggCounter()
  {
     // Required to initialize variables
     InitializeComponent();

     eggTimer = new DispatcherTimer();
     eggTimer.Interval = TimeSpan.FromSeconds(5);
     eggTimer.Tick += new EventHandler(eggTimer_Tick);
     eggTimer.Start();

     Eggs = 0;
  }

  void eggTimer_Tick(object sender, EventArgs e)
  {
  开发者_开发百科   Eggs += 4;
     Pop.Begin();
     mePop.Play();
  }

  private void mePop_MediaEnded(object sender, RoutedEventArgs e)
  {
     mePop.Position = TimeSpan.FromSeconds(0);
  }

  /// <summary>
  /// The <see cref="Eggs" /> dependency property's name.
  /// </summary>
  public const string EggsPropertyName = "Eggs";
  /// <summary>
  /// Gets or sets the value of the <see cref="Eggs" />
  /// property. This is a dependency property.
  /// </summary>
  public int Eggs
  {
     get
     {
        return (int)GetValue(EggsProperty);
     }
     set
     {
        SetValue(EggsProperty, value);
     }
  }
  /// <summary>
  /// Identifies the <see cref="Eggs" /> dependency property.
  /// </summary>
  public static readonly DependencyProperty EggsProperty = DependencyProperty.Register(EggsPropertyName, typeof(int), typeof(EggCounter), new UIPropertyMetadata(0));
}

The XAML for this code is irrelevant. Then, I place this control on my MainPage, like this :

<my:EggCounter ToolTipService.ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Eggs, StringFormat='{} Our chickens have laid {0} eggs since you have been here.'}"/>

The page loads fine, but as soon as the timer fires, I get this error :

"The given key was not present in the dictionary."

in the Eggs property's setter in my usercontrol, i.e. on this line :

SetValue(EggsProperty, value);

I've also tried an ElementBinding on the control, but get the same error. Am I doing something wrong with the dependency property?


Your code contains UIPropertyMetaData. Silverlight does not have this class it uses just PropertyMetaData instead.

Having said that the failure mode you describe seems to indicate that your code compiles, I don't understand how even got that far.


If use your code I don't get the error.

But I didn't had the lines

Pop.Begin();
mePop.Play();

in the TimerCallback. So propably you should seek the error in these two methods..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜