事件处理程序不在xaml中工作

时间:2014-01-27 15:29:54

标签: c# wpf xaml event-handling

我的页面中有TextBox。那有INotifyChangeProperty

所有事件处理程序(LostFocus,KeyDown,KeyUp)都不会触发。在XAML的后面我有C#代码。

private void TextBox_KeyUp_For_Decimal(object sender, KeyEventArgs e)
{
}

这是XAML代码

<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>

在我添加x:文本框的键和添加的样式之前,这是有效的。

1 个答案:

答案 0 :(得分:1)

发现问题。不知何故,由于XAML虚拟化,处理程序没有附加到事件。使用样式明确地设置它们可以解决问题。

就像这样将它添加到TextBox或本例中的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>