I have a custom control with two DependencyProperties. One of type object, which allows the user to add custom content like other controls and one of type string which is used in a Textbox:
public object NoResultContent
{
get { return (object)GetValue(NoResultContentProperty); }
set { SetValue(NoResultContentProperty, value); }
}
public static readonly DependencyProperty NoResultContentProperty =
DependencyProperty.Register("NoResultContent", typeof(object), typeof(AdvancedAutoCompleteBox), new PropertyMetadata(null));
public string FilterText
{
get { return (string)GetValue(FilterTextProperty); }
set { SetValue(FilterTextProperty, value); }
}
public static readonly DependencyProperty FilterTextProperty =
DependencyProperty.Register("FilterText", typeof(string), typeof(AdvancedAutoCompleteBox),
new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal,
new PropertyChangedCallback(OnFilterTextPropertyChanged), new CoerceValueCallback(CoerceText),
true, UpdateSourceTrigger.PropertyChanged));
The ControlTemplate
looks like:
<ControlTemplate
TargetType="{x:Type local:SpecialBox}">
<StackPanel>
<TextBox
Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=FilterText}" />
<ContentPresenter
ContentSource="NoResultContent" />
</StackPanel>
</ControlTemplate>
I'm using it like this:
<Controls:SpecialBox
Name="Box">
<Controls:SpecialBox.NoResultContent>
<Button
Content="Add value"
CommandParameter="{Binding ElementName=Box, Path=FilterText}"
Command="{Binding AddProject}" />
</Controls:SpecialBox.NoResultContent>
</Controls:SpecialBox>
<TextBlock Text="{Binding ElementName=Box, Path=FilterText}" />
The DataContext
of my Window
is set to my ViewModel. So Binding to the ICommand
works. Providing a constant string as a CommandParameter will pass it as desired to the ICommand.
CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Content}"
will pass "Add value" to my ICommand implementation.
The ElementName Binding, as stated above and following code CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Controls:SpeicalBox}, Path=FilterText}"
does not work. The source can not be found.
CommandParameter="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=FilterText}"
does not throw a warning, but always return null.
Further info:
The OnFilterTextPropertyChanged Event of my dp is fired on each change. So the value is available, that's why the TextBlock
s Text Binding to the SpecialBox works quite well.
Providing a second Property on my ViewModel for the FilterText value would be a workaround, but how am I able to access the local dp from that second property?
Button.CommandParameter
is actuallynull
or not (the fault might just as well be in badICommand
implementation). E.g. add aButton.Click
handler, set a breakpoint and inspect theButton
. – Grx70ContentPresenter
)? Also, what framework version are you using? – Grx70