question

ThomasLu-7439 avatar image
0 Votes"
ThomasLu-7439 asked ·

Binding data calculated from DependencyProperty value

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);
windows-uwpwindows-uwp-xaml
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichardZhang-MSFT avatar image
0 Votes"
RichardZhang-MSFT answered ·

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.

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ThomasLu-7439 avatar image
0 Votes"
ThomasLu-7439 answered ·

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);
      }
  }


· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.