简体   繁体   English

依赖项属性更改时,在UWP模板控件中更新UI

[英]Update UI in UWP Template Control when dependency property changes

I want template control that dynamically generates different shapes according to a dependency property.The control looks like this: 我希望模板控件根据依赖项属性动态生成不同的形状。该控件如下所示:

<Style TargetType="local:ColorShape">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:ColorShape">
                <ContentControl x:Name="shapeParent">
                </ContentControl>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

It has a depdency property for the shape: public ShapeType 它具有形状的依赖属性:public ShapeType

ShapeType
{
    get { return (ShapeType)GetValue(ShapeTypeProperty); }
    set { SetValue(ShapeTypeProperty, value); }
}

public static readonly DependencyProperty ShapeTypeProperty =
    DependencyProperty.Register("ShapeType", typeof(ShapeType), typeof(ColorShape), new PropertyMetadata(ShapeType.Circle));

I generate the shape in the OnApplyTemplate method: 我在OnApplyTemplate方法中生成形状:

protected override void OnApplyTemplate()
{
    var shapeParent = (ContentControl)this.GetTemplateChild("shapeParent");
    var shape = GetShape(ShapeType);
    shapeParent.Content = shape;

    base.OnApplyTemplate();
}

I can DataBind the property, and it works the first time I create the control: 我可以使用DataBind属性,并且在我第一次创建控件时可以使用:

<Controls:ColorShape Grid.Row="1" Width="200" Height="200" Stroke="Black" ShapeType="{x:Bind ViewModel.Shape, Mode=OneWay}" StrokeThickness="5" Fill="{x:Bind ViewModel.Color, Mode=OneWay}" />

But if I modify the bound property in the ViewModel, that generates an INotifyPropertyChange notification but doesn't repaints the template control 但是,如果我修改ViewModel中的bound属性,则会生成INotifyPropertyChange通知,但不会重新绘制模板控件

I tried to add a callback in the dependency property of the template control, and I can see it refreshes the dependency property with the new values bound to the property (in this case ViewModel.Shape ), but it doesn't refresh the UI (never calls again OnApplyTemplate ). 我试图在模板控件的dependency属性中添加一个回调,并且可以看到它使用绑定到该属性的新值刷新了该依赖属性(在本例中为ViewModel.Shape ),但没有刷新UI(再也不会调用OnApplyTemplate )。 I tried to manually call ApplyTemplate method, and the event OnApplyTemplate doesn't get fired ever. 我试图手动调用ApplyTemplate方法,并且事件OnApplyTemplate从未触发过。

OnApplyTemplate is only called once, when the template is generated. 生成模板时,仅调用一次OnApplyTemplate It will only be called again, when you change the Template on the control. 仅当您更改控件上的模板时,才会再次调用它。

What you need is a PropertyChangedCallback on the DependencyProperty: 您需要的是DependencyProperty上的PropertyChangedCallback

public int MyProperty
{
    get { return (int)GetValue(MyPropertyProperty); }
    set { SetValue(MyPropertyProperty, value); }
}

public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0, OnPropertyChanged);

private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    // Do what you need here
}

(The part you were missing is the second parameter at new PropertyMetadata(...) . (您缺少的部分是new PropertyMetadata(...)的第二个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM