Edit: I changed the code according to Thorstens Answer, using the enum, but did not work.
I am using Dependency Properties to influence a WPF control I am creating. I’m new to WPF, so I’m not sure what I am doing wrong and I can’t find proper articles explaining it.
For example, I’m trying to define the Visibility of a control via Dep Properties. The property, in this case, would be this:
public static readonly DependencyProperty IconVisibilityBoldProperty =
DependencyProperty.Register("IconVisibilityBold", typeof(Visibility), typeof(RTFBox),
new PropertyMetadata(Visibility.Hidden), VisibilityValidateCallback);
private static bool VisibilityValidateCallback(object value)
{
Visibility prop = (Visibility) value;
if (prop == Visibility.Hidden || prop == Visibility.Visible)
{
return true;
}
return false;
}
public Visibility IconVisibilityBold
{
get
{
return (Visibility)GetValue(IconVisibilityBoldProperty);
}
set
{
SetValue(IconVisibilityBoldProperty, value);
}
}
Edit: for correct XAML, look for Slugarts answer.
The XAML Entry for this, in this case a ToggleButton, would be
<ToggleButton Visibility="{Binding Path=IconVisibilityBold}" ToolBar.OverflowMode="Never" x:Name="ToolStripButtonBold" Command="EditingCommands.ToggleBold" ToolTip="Bold">
<Image Source="Images\Bold.png" Stretch="None"/>
</ToggleButton>
I’ve output the Property, it shows as “Hidden” as the Metadata Default Value should imply, but apparently I’ve done something wrong with the binding. What would I have to write there?
You are trying to binding to a property of the parent control without referencing it, and it won’t be set implicitly. You need to set the ElementName in the ToggleButton binding to be the name of the UserControl you are creating (giving it an x:Name property if it doesn’t have one already).
Also you should follow the previous answers which correctly state that the Visibility property is an enum and not a string.