I am going to create a custom control - specifically a pie chart. I want the markup to look something like this:
<c:PieChart>
<c:Slice Value="{Binding RedCount}" />
<c:Slice Value="{Binding BlueCount}" />
<c:Slice Value="{Binding GreenCount}" />
</c:PieChart>
PieChartcomes from Control:
[ContentProperty("Slices")]
public class PieChart : Control
{
public PieChart()
{
Slices = new ObservableCollection<Slice>();
}
public ObservableCollection<Slice> Slices { get; private set; }
}
The aforementioned XAML causes the property to Slicespopulate with three instances Slice.
Here's a snippet from Slice:
public class Slice : ContentControl
{
public static DependencyProperty ValueProperty
= DependencyProperty.Register(
"Value", typeof(double), typeof(Slice),
new PropertyMetadata((p,a) => ((Slice)p).OnValueChanged(a)));
private void OnValueChanged(DependencyPropertyChangedEventArgs e)
{
}
}
The problem is that the property Valueis Slicenever set. If I put the dependency property in PieChartand copied the binding expression, then the value will be set, so I'm sure that I understand the basics of dependency and binding properties.
, , ? , - , - , ?
" " , , , , .
, DataContext PieChart , RedCount .. , ( - ), VS . .