I'm creating a custom control, and I want to add a dependency property which works like CheckBox.IsChecked, ie when the user clicks the control the binding will update the source. The declaration for this custom control is as follows:
public class QuadStateSelector : Control
{
static QuadStateSelector()
{
PropertyChangedCallback callback = new PropertyChangedCallback(OnValueChanged);
FrameworkPropertyMetadata md = new FrameworkPropertyMetadata(0, callback);
md.BindsTwoWayByDefault = true;
md.DefaultUpdateSourceTrigger = System.Windows.Data.UpdateSourceTrigger.PropertyChanged;
ValueProperty = DependencyProperty.Register(nameof(Value), typeof(int), typeof(QuadStateSelector), md);
DefaultStyleKeyProperty.OverrideMetadata(typeof(QuadStateSelector), new FrameworkPropertyMetadata(typeof(QuadStateSelector)));
}
private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
public int Value
{
get { return (int)this.GetValue(ValueProperty); }
set { this.SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty;
As you can see, the default binding is 2 way, triggered on a source changed. I've also added a callback so that I can see when the property changes.
My XAML is:
<rt:QuadStateSelector Name="qss" Value="{Binding LineState}" Canvas.Left="262" Canvas.Top="10" Background="Azure"/>
<xctk:IntegerUpDown Value="{Binding ElementName=qss, Path=Value }"/>
And my source is:
public int LineState
{
get => _LineState;
set => _LineState = value; // This line is never called
}
Changes to the value of the dependency property are being made by event triggers in the control template, as below:
<Style TargetType="{x:Type local:QuadStateSelector}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:QuadStateSelector}">
<StackPanel Orientation="Horizontal" Background="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}">
<Ellipse Margin="1,0,1,0" Name="Button1" Width="14" Height="14" Stroke="{Binding Button1Brush, RelativeSource={RelativeSource TemplatedParent}}" StrokeThickness="2" Fill="AntiqueWhite"/>
<Ellipse Margin="1,0,1,0" Name="Button2" Width="14" Height="14" Stroke="{Binding Button2Brush, RelativeSource={RelativeSource TemplatedParent}}" StrokeThickness="2" Fill="AntiqueWhite"/>
<Ellipse Margin="1,0,1,0" Name="Button3" Width="14" Height="14" Stroke="{Binding Button3Brush, RelativeSource={RelativeSource TemplatedParent}}" StrokeThickness="2" Fill="AntiqueWhite"/>
<Ellipse Margin="1,0,1,0" Name="Button4" Width="14" Height="14" Stroke="{Binding Button4Brush, RelativeSource={RelativeSource TemplatedParent}}" StrokeThickness="2" Fill="AntiqueWhite"/>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="Value" Value="0">
<Setter TargetName="Button1" Property="Fill" Value="{Binding Button1Brush, RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
<Trigger Property="Value" Value="1">
<Setter TargetName="Button2" Property="Fill" Value="{Binding Button2Brush, RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
<Trigger Property="Value" Value="2">
<Setter TargetName="Button3" Property="Fill" Value="{Binding Button3Brush, RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
<Trigger Property="Value" Value="3">
<Setter TargetName="Button4" Property="Fill" Value="{Binding Button4Brush, RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
<EventTrigger RoutedEvent="Control.MouseDown" SourceName="Button1">
<BeginStoryboard>
<Storyboard>
<Int32AnimationUsingKeyFrames Storyboard.TargetProperty="Value" Duration="0:0:1">
<DiscreteInt32KeyFrame KeyTime="0" Value="0"/>
</Int32AnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Control.MouseDown" SourceName="Button2">
<BeginStoryboard>
<Storyboard>
<Int32AnimationUsingKeyFrames Storyboard.TargetProperty="Value" Duration="0:0:1">
<DiscreteInt32KeyFrame KeyTime="0" Value="1"/>
</Int32AnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Control.MouseDown" SourceName="Button3">
<BeginStoryboard>
<Storyboard>
<Int32AnimationUsingKeyFrames Storyboard.TargetProperty="Value" Duration="0:0:1">
<DiscreteInt32KeyFrame KeyTime="0" Value="2"/>
</Int32AnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Control.MouseDown" SourceName="Button4">
<BeginStoryboard>
<Storyboard>
<Int32AnimationUsingKeyFrames Storyboard.TargetProperty="Value" Duration="0:0:1">
<DiscreteInt32KeyFrame KeyTime="0" Value="3"/>
</Int32AnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
There are two interconnected controls and when I run the application, changes in one control are reflected in the other, but only changes made in the IntegerUpDown cause the setter to be triggered. So the event triggers are definitely modifying the dependency property because (a) the property changed callback gets called, and (b) the value in the IntegerUpDown (bound to the dependency property) is updated.
What am I missing here?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.