I can't reproduce your error , could you reproduce this issue based on my sample code?And could you tell me what framwork did your project target?
MainWindow.xaml.cs:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
//State = true;
var test = tstFilter.IsChecked;
}
bool state;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
public bool State
{
get { return state; }
set { state = value;
//this.OnPropertyChanged("State");
}
}
public override void OnApplyTemplate()
{
tstFilter.IsChecked = true;
base.OnApplyTemplate();
}
}
MainWindow.Xaml
<Window x:Class="teststate.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:teststate"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<local:filter Content="Test" x:Name="tstFilter" IsChecked="{Binding State, Mode=OneWayToSource}" >
</local:filter>
<TextBlock Text="{Binding IsChecked, ElementName=tstFilter}" Height="50" />
<TextBlock Text="{Binding State}" Foreground="Red" Height="50" Margin="0,226,0,146.5" />
</Grid>
</Window>
Custom Control filter.cs:
public class filter : CheckBox
{
static filter()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(filter), new FrameworkPropertyMetadata(typeof(filter)));
}
}
Generic.xaml:
<Style TargetType="{x:Type local:filter}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:filter}">
<Grid>
<Border Background="Black"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Width="100" Height="100">
</Border>
<TextBlock Text="test"></TextBlock>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="IsThreeState" Value="False"/>
<Setter Property="IsChecked" Value="True"/>
</Style>
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.