Using:
- Visual Studio Community Edition 2015
- .Net 4.0
I've implemented this answer, producing my own CheckBox
class complete with an IsChecked
DependencyProperty
. That property is backed by the IsChecked
property on the WPF CheckBox
, or would be if it would work. Working would mean my getter and setter are called when the checkbox is toggled.
If I rename my property to IsChecked_temp
and modify the XAML to match, it works fine. I think this is a naming conflict, but why doesn't ElementName
resolve it? My minimal test case follows.
EDIT 0: I forgot to mention, I get no errors or warnings.
EDIT 1: This answer was initially accepted because it works for the test case, but it's apparently not the entire answer. Applying it to my project (and renaming the CheckBox
class to ToggleBox
) yields a XamlParseException
at every use of the property:
A 'Binding' cannot be set on the 'IsChecked' property of type 'ToggleBox'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
I'll try to get a minimal test case going to show this.
CheckBox.xaml
<UserControl x:Class="CheckBox_test.CheckBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Self">
<StackPanel>
<CheckBox IsChecked="{Binding IsChecked, ElementName=Self}" />
</StackPanel>
</UserControl>
CheckBox.xaml.cs
using System.Windows;
using System.Windows.Controls;
namespace CheckBox_test
{
public partial class CheckBox : UserControl
{
public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register(
"IsChecked",
typeof(bool),
typeof(CheckBox),
new FrameworkPropertyMetadata(false,
FrameworkPropertyMetadataOptions.AffectsRender));
public bool IsChecked
{
get { return (bool)GetValue(IsCheckedProperty); }
set { SetValue(IsCheckedProperty, value); }
}
public CheckBox()
{
InitializeComponent();
}
}
}
MainWindow.xaml
<Window x:Class="CheckBox_test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CheckBox_test">
<Grid>
<local:CheckBox />
</Grid>
</Window>
MainWindow.xaml.cs (for completeness)
using System.Windows;
namespace CheckBox_test
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}