1 year ago
#75375
user2396632
Binding dependency property to attached property in control template
I have created attached properties with data that I want to use inside a button's control template. One such property describes current progress. I have a UserControl called ProgressControl which displays current progress visually. It has a Progress DependencyProperty. I have created a button UserControl, with ProgressControl inside of it and I want to bind ProgressControl's Progress property to the attached property, so I can specify current progress in a button using the ControlTemplate. I am getting some binding errors and I do not understand why.
Here are some code samples: The attached property:
public class AttachedPropertiesSample
{
public static Visibility GetProgressVisibility(DependencyObject obj)
{
return (Visibility)obj.GetValue(ProgressVisibilityProperty);
}
public static void SetProgressVisibility(DependencyObject obj, Visibility value)
{
obj.SetValue(ProgressVisibilityProperty, value);
}
public static readonly DependencyProperty ProgressVisibilityProperty =
DependencyProperty.RegisterAttached("ProgressVisibility", typeof(Visibility), typeof(AttachedPropertiesSample), new PropertyMetadata(Visibility.Collapsed));
public static double GetProgress(DependencyObject obj)
{
return (float)obj.GetValue(ProgressProperty);
}
public static void SetProgress(DependencyObject obj, float value)
{
obj.SetValue(ProgressProperty, value);
}
public static readonly DependencyProperty ProgressProperty =
DependencyProperty.RegisterAttached("Progress", typeof(float), typeof(AttachedPropertiesSample), new PropertyMetadata(0.0f));
}
ProgressControl dependency property:
public partial class ProgressControl : UserControl
{
public static readonly DependencyProperty ProgressProperty = DependencyProperty.Register(
"Progress", typeof(float), typeof(ProgressControl), new PropertyMetadata(0.5f, new PropertyChangedCallback(OnProgressChanged)));
public float Progress
{
get { return (float)GetValue(ProgressProperty); }
set { SetValue(ProgressProperty, value); }
}
Assigning attached properties in xaml:
<Button
local:AttachedPropertiesSample.ProgressVisibility="Visible"
local:AttachedPropertiesSample.Progress="0.5"
Template="{DynamicResource ButtonTemplate1}" x:Name="button" Content="Button" HorizontalAlignment="Left" Height="75" Margin="239,142,0,0" VerticalAlignment="Top" Width="215"/>
Button control template 1:
<ControlTemplate x:Key="ButtonTemplate1" TargetType="{x:Type Button}">
<Grid Background="Green">
<local:ProgressControl
Visibility="{Binding (local:AttachedPropertiesSample.ProgressVisibility), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"
Progress="{Binding (local:AttachedPropertiesSample.Progress), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"
/>
</Grid>
</ControlTemplate>
In the case of this control template both bindings fail: (local:AttachedPropertiesSample.ProgressVisibility) property not found on object of type Button. (local:AttachedPropertiesSample.Progress) property not found on object of type Button.
I have modified the control template by adding another control with visibility bound to the same path. And now only the Progress property fails to bind, visibility binding works on both.
Control template 2 (with added TestControl binding to the same attached property):
<ControlTemplate x:Key="ButtonTemplate1" TargetType="{x:Type Button}">
<Grid Background="Green">
<local:TestControl
Visibility="{Binding (local:AttachedPropertiesSample.ProgressVisibility), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"
/>
<local:ProgressControl
Visibility="{Binding (local:AttachedPropertiesSample.ProgressVisibility), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"
Progress="{Binding (local:AttachedPropertiesSample.Progress), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"
/>
</Grid>
</ControlTemplate>
After adding the TestControl above ProgressControl and binding its visibility to the same attached proprety, the binding works for both (there is only one binding error left, the one for Progress property).
I do not understand why the binding errors appear in the first place. I also have no idea why adding the other object with the binding fixes the problem for one of the bindings. I'd appreciate an explanation as to what is going on here and how I could make the bindings work properly.
c#
wpf
0 Answers
Your Answer