在数据模板外.propertyvaluechange不运行

本文关键字:propertyvaluechange 运行 数据 | 更新日期: 2023-09-27 18:04:19

我有一个自定义控件,它使用On(propertyname)ValueChanged从字典中读取条目,并为该控件设置参数。

我还想单独使用这个控件,而不仅仅是一个绑定控件。

那么为什么onpropertyvaluechange只在数据集中工作?

依赖属性只从xaml工作,这是否意味着我将不得不从容器类的属性绑定?(可能已经回答了我自己的问题)

>

    <local:spriteToggleButton x:Name="testButton" HorizontalAlignment="Center" Text="{Binding testString, ElementName=mainPage}" Correct="true" Margin="93,561,93,63" Grid.Row="1" Sprites="{Binding testSprites, ElementName=mainPage}" />
在mainpage.xaml.cs

    testSprites.Add("idle", idlesprite); // a dictionary of a custom sprite object
    testSprites.Add("highlighted", highlightedsprite);
    testSprites.Add("selected", selectedsprite);
    testString = "this is a test"; // this property is picked up by the binding.

当我从绑定中添加精灵时,它会运行依赖属性更改回调,但是当控件单独使用时,spriteToggleButton类中的属性不会更新

这是我的依赖属性更改回调

    private static void OnSpritesPropertyValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = d as spriteToggleButton;
        var sprites = e.NewValue as Dictionary<string, Quiz.Sprite>;
        control.idleSprite = sprites["idle"];
        control.selectedSprite = sprites["selected"];
        control.highlightedSprite = sprites["highlighted"];
    }

这是我的spriteToggleButton

 <local:spriteView x:Name="Idle" Width="294" Height="57" HorizontalAlignment="Center" Sprite="{Binding idleSprite, ElementName=toggleSpriteControl}"  />
 ...

Sprite也是控件中的一个依赖属性

在数据模板外.propertyvaluechange不运行

我非常怀疑你没有实现spriteToggleButton类的idleSprite, selectedSpritehighlightedSprite作为依赖属性。

对于它的价值看来,你正在使用UserControl实现spriteToggleButton,我将从ToggleButton派生并替换默认模板。

相关文章:
  • 没有找到相关文章