Reputation: 33
I have a TextBox
in my page. That has INotifyChangeProperty
All the event handlers (LostFocus, KeyDown, KeyUp) will not fire. Behind the XAML I have C# code.
private void TextBox_KeyUp_For_Decimal(object sender, KeyEventArgs e)
{
}
Here is the XAML code
<Grid Visibility="{Binding FreeInput, Converter={StaticResource BoolToVisibilityConverter}}">
<Grid.Resources>
<TextBox
x:Key="StringDefaultPresenter"
x:Shared="False"
Text="{Binding Default, UpdateSourceTrigger=LostFocus}" CommandManager.PreviewExecuted="TextBox_PreviewExecuted"
LostFocus="Update_Default"
KeyDown="TextBox_KeyDown_For_String"/>
<TextBox
x:Key="NumberDefaultPresenter"
x:Shared="False"
Text="{Binding Default, UpdateSourceTrigger=LostFocus}"
CommandManager.PreviewExecuted="TextBox_PreviewExecuted"
LostFocus="Update_Default"
KeyDown="TextBox_KeyDown_For_Decimal"
KeyUp="TextBox_KeyUp_For_Decimal"/>
<CheckBox
x:Key="BooleanDefaultPresenter"
x:Shared="False"
HorizontalAlignment="Center"
VerticalAlignment="Center"
LostFocus="Update_Default"
IsChecked="{Binding Default, Converter={l:DefaultValueConverter}, UpdateSourceTrigger=LostFocus}"/>
<DatePicker
x:Key="DateTimeDefaultPresenter"
x:Shared="False"
SelectedDate="{Binding Default, Converter={l:DefaultValueConverter}, UpdateSourceTrigger=LostFocus}"
LostFocus="Update_Default"/>
<Style x:Key="DefaultPresenterStyle" x:Shared="False" TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding DataType}" Value="{x:Static l:DataType.STRING}">
<Setter Property="Content" Value="{StaticResource StringDefaultPresenter}"/>
</DataTrigger>
<DataTrigger Binding="{Binding DataType, Converter={l:EnumMatchToBoolConverter}, ConverterParameter={StaticResource NumberDataType}}" Value="True">
<Setter Property="Content" Value="{StaticResource NumberDefaultPresenter}"/>
</DataTrigger>
<DataTrigger Binding="{Binding DataType}" Value="{x:Static l:DataType.BOOLEAN}">
<Setter Property="Content" Value="{StaticResource BooleanDefaultPresenter}"/>
</DataTrigger>
<DataTrigger Binding="{Binding DataType}" Value="{x:Static l:DataType.DATE_TIME}">
<Setter Property="Content" Value="{StaticResource DateTimeDefaultPresenter}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<ContentControl Style="{StaticResource DefaultPresenterStyle}"/>
</Grid>
This was working before I added x:Keys to the textboxes and added styles.
Upvotes: 0
Views: 1288
Reputation: 33
Found the problem. Somehow because of XAML virtualization the handlers are not attached to the events.Setting them explicitly using styles solves the problem.
Like so add this to a TextBox or in this case a ComboBox
<ComboBox
x:Key="StringLengthPresenter" x:Shared="False"
SelectedItem="{Binding Length, UpdateSourceTrigger=PropertyChanged}">
<ComboBox.Style>
<Style TargetType="ComboBox">
<EventSetter Event="SelectionChanged" Handler="Length_SelectionChanged"/>
</Style>
</ComboBox.Style>
Upvotes: 1