检查的事件是触发 "late" 还是 "early"?
Are checked events firing "late" or "early"?
我在 Telerik RadJumpList 的 ItemTemplate 中使用 Checkbox 控件。
ItemTemplate 定义如下:
<DataTemplate x:Key="ItemTemplate">
<StackPanel Orientation="Horizontal"
Margin="4">
<Image Source="/Assets/Weather.png"
Width="64"
Height="64"
Margin="0,4,12,4" Stretch="Fill" />
<CheckBox x:Name="checkBox"
IsChecked="{Binding IsChecked, Mode=TwoWay}"
Checked="checkBox_CheckedChanged"
Unchecked="checkBox_CheckedChanged" />
<TextBlock FontSize="{StaticResource PhoneFontSizeExtraLarge}"
FontFamily="{StaticResource PhoneFontFamilyLight}"
Text="{Binding Name}"
VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
JumpList 是这样定义的:
<telerikDataControls:RadJumpList Grid.Row="1"
x:Name="FavoriteJumpList"
ItemTemplate="{StaticResource ItemTemplate}"
GroupHeaderTemplate="{StaticResource GroupHeaderTemplate}"
StickyHeaderTemplate="{StaticResource StickyHeaderTemplate}"
GroupPickerItemsPanel="{StaticResource ItemsPanelTemplate}"
ItemTap="FavoriteJumpList_ItemTap">
<telerikDataControls:RadJumpList.GroupPickerItemTemplateSelector>
<local:PhoneBookTemplateSelector x:Name="TemplateSelector"
EmptyItemTemplate="{StaticResource EmptyItemTemplate}"
LinkedItemTemplate="{StaticResource LinkedItemTemplate}" />
</telerikDataControls:RadJumpList.GroupPickerItemTemplateSelector>
</telerikDataControls:RadJumpList>
事件处理器定义如下:
private void checkBox_CheckedChanged( object sender, RoutedEventArgs e )
{
var count = _app.AppData.Favorites.FavoritePoints.Count( favorite => favorite.IsChecked );
WriteLine( "Count {0}", count );
}
在页面的c#中,JumpList是这样初始化的:
FavoriteJumpList.IsAsyncBalanceEnabled = true;
FavoriteJumpList.IsStickyHeaderEnabled = true;
FavoriteJumpList.GroupCheckMode = GroupCheckMode.None;
FavoriteJumpList.ItemCheckedStateChanged += FavoriteJumpList_ItemCheckedStateChanged;
// add custom group picker items, including all alphabetic characters
var groupPickerItems = new List<string>( 32 );
foreach ( var c in Alphabet )
{
groupPickerItems.Add( new string( c, 1 ) );
}
FavoriteJumpList.GroupPickerItemsSource = groupPickerItems;
// add the group and sort descriptors
var groupByFirstName = new GenericGroupDescriptor<FavoriteLocation, string>( favoriteLocation => favoriteLocation.Name.Substring( 0, 1 ).ToLower() );
FavoriteJumpList.GroupDescriptors.Add( groupByFirstName );
var sort = new GenericSortDescriptor<FavoriteLocation, string>( favoriteLocation => favoriteLocation.Name );
FavoriteJumpList.SortDescriptors.Add( sort );
// assign data source
LoadFavorites();
FavoriteJumpList.ItemsSource = _app.AppData.Favorites.FavoritePoints;
我放了两个断点。一进checkBox_CheckedChanged就行了:
WriteLine( "Count {0}", count );
还有一个上线 RaisePropertyChanged:
private bool _isChecked;
[DataMember]
public bool IsChecked
{
get
{
return _isChecked;
}
set
{
_isChecked = value;
RaisePropertyChanged( "IsChecked" );
}
}
两个断点都触发了。但是,我得到了断点
checkBox_CheckedChanged()
被触发之前
RaisePropertyChanged( "IsChecked" );
被触发,这是不可接受的(也是意外的)。
中的计数变量
checkBox_CheckedChanged()
用于Enable/Disable工具栏按钮,计数滞后,显示错误值。
知道为什么这里触发了错误的事件顺序(或延迟)吗?我该如何正确处理它?
感谢
MJW 的回答是正确的。但是,为了解决我的问题,我修改了代码,以便在触发 CheckBox 单击事件时完成计数计算。
它在我的私人案例中有效,但我不确定在数据已经更新(即绑定已经完成)后触发点击事件是否是一个规则。
谢谢。
我在 Telerik RadJumpList 的 ItemTemplate 中使用 Checkbox 控件。 ItemTemplate 定义如下:
<DataTemplate x:Key="ItemTemplate">
<StackPanel Orientation="Horizontal"
Margin="4">
<Image Source="/Assets/Weather.png"
Width="64"
Height="64"
Margin="0,4,12,4" Stretch="Fill" />
<CheckBox x:Name="checkBox"
IsChecked="{Binding IsChecked, Mode=TwoWay}"
Checked="checkBox_CheckedChanged"
Unchecked="checkBox_CheckedChanged" />
<TextBlock FontSize="{StaticResource PhoneFontSizeExtraLarge}"
FontFamily="{StaticResource PhoneFontFamilyLight}"
Text="{Binding Name}"
VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
JumpList 是这样定义的:
<telerikDataControls:RadJumpList Grid.Row="1"
x:Name="FavoriteJumpList"
ItemTemplate="{StaticResource ItemTemplate}"
GroupHeaderTemplate="{StaticResource GroupHeaderTemplate}"
StickyHeaderTemplate="{StaticResource StickyHeaderTemplate}"
GroupPickerItemsPanel="{StaticResource ItemsPanelTemplate}"
ItemTap="FavoriteJumpList_ItemTap">
<telerikDataControls:RadJumpList.GroupPickerItemTemplateSelector>
<local:PhoneBookTemplateSelector x:Name="TemplateSelector"
EmptyItemTemplate="{StaticResource EmptyItemTemplate}"
LinkedItemTemplate="{StaticResource LinkedItemTemplate}" />
</telerikDataControls:RadJumpList.GroupPickerItemTemplateSelector>
</telerikDataControls:RadJumpList>
事件处理器定义如下:
private void checkBox_CheckedChanged( object sender, RoutedEventArgs e )
{
var count = _app.AppData.Favorites.FavoritePoints.Count( favorite => favorite.IsChecked );
WriteLine( "Count {0}", count );
}
在页面的c#中,JumpList是这样初始化的:
FavoriteJumpList.IsAsyncBalanceEnabled = true;
FavoriteJumpList.IsStickyHeaderEnabled = true;
FavoriteJumpList.GroupCheckMode = GroupCheckMode.None;
FavoriteJumpList.ItemCheckedStateChanged += FavoriteJumpList_ItemCheckedStateChanged;
// add custom group picker items, including all alphabetic characters
var groupPickerItems = new List<string>( 32 );
foreach ( var c in Alphabet )
{
groupPickerItems.Add( new string( c, 1 ) );
}
FavoriteJumpList.GroupPickerItemsSource = groupPickerItems;
// add the group and sort descriptors
var groupByFirstName = new GenericGroupDescriptor<FavoriteLocation, string>( favoriteLocation => favoriteLocation.Name.Substring( 0, 1 ).ToLower() );
FavoriteJumpList.GroupDescriptors.Add( groupByFirstName );
var sort = new GenericSortDescriptor<FavoriteLocation, string>( favoriteLocation => favoriteLocation.Name );
FavoriteJumpList.SortDescriptors.Add( sort );
// assign data source
LoadFavorites();
FavoriteJumpList.ItemsSource = _app.AppData.Favorites.FavoritePoints;
我放了两个断点。一进checkBox_CheckedChanged就行了:
WriteLine( "Count {0}", count );
还有一个上线 RaisePropertyChanged:
private bool _isChecked;
[DataMember]
public bool IsChecked
{
get
{
return _isChecked;
}
set
{
_isChecked = value;
RaisePropertyChanged( "IsChecked" );
}
}
两个断点都触发了。但是,我得到了断点
checkBox_CheckedChanged()
被触发之前
RaisePropertyChanged( "IsChecked" );
被触发,这是不可接受的(也是意外的)。
中的计数变量checkBox_CheckedChanged()
用于Enable/Disable工具栏按钮,计数滞后,显示错误值。
知道为什么这里触发了错误的事件顺序(或延迟)吗?我该如何正确处理它?
感谢
MJW 的回答是正确的。但是,为了解决我的问题,我修改了代码,以便在触发 CheckBox 单击事件时完成计数计算。
它在我的私人案例中有效,但我不确定在数据已经更新(即绑定已经完成)后触发点击事件是否是一个规则。
谢谢。