I\'m struggling to get my head around dependency properties in WPF, maybe because the use case I\'m looking for is very specific and not well documented.
What I have is
There isn't a dependency property in your code.
This is a dependency property:
public partial class FillGraph : UserControl
{
public FillGraph()
{
InitializeComponent();
}
public float Percentage
{
get { return (float)GetValue(PercentageProperty); }
set { SetValue(PercentageProperty, value); }
}
// Using a DependencyProperty as the backing store for Percentage. This
// enables animation, styling, binding, etc...
public static readonly DependencyProperty PercentageProperty =
DependencyProperty.Register("Percentage", typeof(float),
typeof(FillGraph), new PropertyMetadata(0.0f));
}
As Ayyappan Subramanian suggests, the propdp
snippet in Visual Studio will help create the boilerplate. Just be careful with the parameters you pass to DependencyProperty.Register()
, and make sure the default value you pass to new PropertyMetadata()
is the correct type. 0.0f
is a float. If you pass integer 0
for a float
property, it'll throw an exception at runtime.
The regular property public float Percentage
here is optional. It's just there for your code to use. XAML won't ever touch it at all (put breakpoints in the getter and setter if you doubt me). That's part of what's special about dependency properties.
And here's how to use it in your usercontrol XAML. Note the StringFormat
parameter to the binding.
<Grid>
<TextBlock
Text="{Binding Percentage, RelativeSource={RelativeSource AncestorType=UserControl}, StringFormat='Fill Percentage: {0:#.##}%'}"
/>
</Grid>
Note: If your percentage is expressed in the range 0 to 1 rather than 0 to 100, use the p
percent format instead. We'll use p2
for two digits after the decimal point. We omit the %
because the format string provides that.
<TextBlock
Text="{Binding Percentage, StringFormat='Fill Percentage: {0:p2}', RelativeSource={RelativeSource AncestorType=UserControl}}"
/>
This XAML from your question is fine just as it is, assuming that the viewmodel has a FillPercentage
property and correctly implements INotifyPropertyChanged
:
<controls:FillGraph x:Name="HydroModel" Percentage="{Binding FillPercentage}" />