Binding data calculated from DependencyProperty value

Thomas Lu 21 Reputation points
2019-12-10T04:41:08.26+00:00

Hi all,

I am attempting to create a custom Templated Control in at UWP app that automatically generates binding data for its child controls based on a provided DependencyProperty specified in the XAML.

For example, imagine I have a Templated Control with the following child controls in the Generic.xaml:

  • TextBlock x:Name="ProductName" Text="{TemplateBinding Name}"
  • TextBlock x:Name="ProductPrice" Text="{TemplateBinding Price}"
  • TextBlock x:Name="DiscountedPrice"

and want to only specify the values for Name and Price in XAML, via

  • myControls:Product Name="Potato" Price="5.00"

while having the price after discount automatically calculated via the Product.cs file.

Is it possible to create bindable data for DiscountedPrice based on Price/ProductPrice?

The issue I'm running into is that the properties generated by "propdp" are non-static, and as a result, I am lost on how to make the calculated value bindable by any method. (I'm coming into UWP straight from WinForms...)

// TemplateBinding Price  
public string Price  
{  
   get { return (string)GetValue(PriceProperty); }  
   set { SetValue(PriceProperty, value); }  
}  
public static readonly DependencyProperty PriceProperty =  
DependencyProperty.Register(nameof(Price), typeof(string), typeof(Product), new PropertyMetadata("0.00"));  
  
// Calculated Value of DiscountedPrice  
public string DiscountedPrice => Convert.ToString(Convert.ToInt32(Price) * 0.8);
Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Richard Zhang-MSFT 6,936 Reputation points
    2019-12-10T06:13:04.7+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You want to dynamically change the value of DiscountedPrice when the Price changes. This is possible.

    You can register a callback method for value changes when creating a DependencyProperty, specifically created as follows:

    public static readonly DependencyProperty PriceProperty =  
    DependencyProperty.Register(nameof(Price), typeof(string), typeof(Product), new PropertyMetadata("0.00",new PropertyChangedCallback(Price_Changed)));  
      
    private static void Price_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)  
    {  
        if(e.NewValue!=null && !e.NewValue.Equals(e.OldValue))  
        {  
            double price = Convert.ToDouble(e.NewValue);  
            double discountedPrice = price * 0.8;  
            //Assign values to TextBlock or perform other operations  
        }  
    }  
    

    Thanks.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Thomas Lu 21 Reputation points
    2019-12-10T20:35:08.707+00:00

    Wow!!

    Thank you Richard; that was it!!

    I'm not sure if I implemented it properly, but this appears to have gotten the job done:

    private static void Price_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)  
     {  
         if(e.NewValue!=null && !e.NewValue.Equals(e.OldValue))  
         {  
             double price = Convert.ToDouble(e.NewValue);  
             double discountedPrice = price * 0.8;  
    
             d.SetValue(DiscountedPrice, (string)discountedPrice);  
         }  
     }  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.