Binding data calculated from DependencyProperty value

  • Thread starter Thread starter Thomas Lu (UBC)
  • Start date Start date
T

Thomas Lu (UBC)

Guest
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

<Style TargetType="local2:Product">
[...]
<ControlTemplate TargetType="local2:Statistic">
[...]
<TextBlock x:Name="ProductName" Text="{TemplateBinding Name}"/>
<Image x:Name="ProductImage" Source="{TemplateBinding Image}"/>
<TextBlock x:Name="ProductPrice" Text="{TemplateBinding Price}"/>
<TextBlock x:Name="PriceAfterDiscount" />
[...]
</ControlTemplate>
[...]
</Style>


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

<myControls:Product Name="Potato" Image="[...]" Price="5.00" />
while having the price after discount automatically calculated via the Product.cs file.



Is it possible to create bindable data for PriceAfterDiscount 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 Value => Convert.ToString(Convert.ToInt32(Base) * 0.8);

Any direction on what I can do to achieve this would be much appreciated!

Continue reading...
 
Back
Top