Reputation: 1
I have a CustomControl in WPF.
[TemplatePart(Name = ControlSaveButtonName, Type = typeof(Button))]
[TemplatePart(Name = ControlCancelButtonName, Type = typeof(Button))]
[TemplatePart(Name = ControlPanelName, Type = typeof(Panel))]
public class BaseEditEntityControl : Control
{
public const string ControlSaveButtonName = "PART_SaveButton";
public const string ControlCancelButtonName = "PART_CancelButton";
public const string ControlPanelName = "PART_Panel";
static BaseEditEntityControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(BaseEditEntityControl), new FrameworkPropertyMetadata(typeof(BaseEditEntityControl)));
}
private Button _saveButtonControl;
public Button SaveButtonControl
{
get { return _saveButtonControl; }
}
private Button _cancelButtonControl;
public Button CancelButtonControl
{
get { return _cancelButtonControl; }
}
private Panel _panelControl;
public Panel PanelControl
{
get { return _panelControl; }
}
public static readonly DependencyProperty EntityProperty = DependencyProperty.Register(
"Entity", typeof(object), typeof(BaseEditEntityControl));
//, new FrameworkPropertyMetadata
//{
// BindsTwoWayByDefault = true,
// PropertyChangedCallback = BaseEntityPropertyChanged
//});
public object Entity
{
get { return GetValue(EntityProperty); }
set { SetValue(EntityProperty, value); FillPropertyControls(); }
}
public BaseEditEntityControl()
{
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_saveButtonControl = GetTemplateChild(ControlSaveButtonName) as Button;
_cancelButtonControl = GetTemplateChild(ControlCancelButtonName) as Button;
_panelControl = GetTemplateChild(ControlPanelName) as Panel;
if (_saveButtonControl != null) _saveButtonControl.Click += SaveButton_Click;
if (_cancelButtonControl != null) _cancelButtonControl.Click += CancelButton_Click;
}
public void FillPropertyControls()
{
//Some Code
}
//private static void BaseEntityPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
//{
// var baseEntityControl = d as BaseEditEntityControl;
// if (baseEntityControl == null) return;
//
// baseEntityControl.Entity = e.NewValue as BaseEntity;
//}
}
Control template:
<Style x:Key="PassportEntityEditControlStyle" TargetType="{x:Type base:BaseEditEntityControl}">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type base:BaseEditEntityControl}">
<StackPanel x:Name="PART_Panel" Orientation="Vertical">
<TextBlock Text="{Binding Entity, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=base:BaseEditEntityControl}}"></TextBlock>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Control using:
<Style TargetType="propertyControls1:PersonPassportPropertyControl">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="propertyControls1:PersonPassportPropertyControl">
<Grid ScrollViewer.CanContentScroll="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<base:BaseEditEntityControl x:Name="PART_Value" ScrollViewer.CanContentScroll="True"
Entity="{Binding PropertyBinding, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=propertyControls1:PersonPassportPropertyControl}}"
Style="{StaticResource PassportEntityEditControlStyle}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
PersonPassportPropertyControl also a custom control.
PROBLEM: TextBox filled, but 'Entity set' and FillPropertyControls not called. If uncomment PropertyChangedCallback then BaseEntityPropertyChanged called.
Upvotes: 0
Views: 854
Reputation: 81313
That's how it's designed to work. It's clearly mentioned in documentation as well that XAML bypasses property wrappers and directly call GetValue
and SetValue
.
From MSDN:
The current WPF implementation of its XAML processor is inherently dependency property aware. The WPF XAML processor uses property system methods for dependency properties when loading binary XAML and processing attributes that are dependency properties. This effectively bypasses the property wrappers. When you implement custom dependency properties, you must account for this behavior and should avoid placing any other code in your property wrapper other than the property system methods GetValue and SetValue.
Use PropertyChangeCallback
instead to place your code which you want to execute whenever dependency property changes.
Upvotes: 1
Reputation: 17125
Bindings do not use get and set accessors.
change the Entity property the following way:
//Entity Dependency Property
public object Entity
{
get { return (object)GetValue(EntityProperty); }
set { SetValue(EntityProperty, value); }
}
public static readonly DependencyProperty EntityProperty =
DependencyProperty.Register("Entity", typeof(object),
typeof(BaseEditEntityControl),
new UIPropertyMetadata(null),
(d, e) => { FillPropertyControls(); });
Upvotes: 1