早一段時間又一次出差青海省西寧市;回來又是總結又是各種瑣事,也沒顧得上去分享點東西;大周末的就在家分享一下,這二天再次基于ListBox實現的合并單元格的效果:
1、ListBox嵌套ListBox的前臺布局:
<ListBox ItemsSource="{Binding LCPListData}" x:Name="ManufacturerListBox"Style="{StaticResource calcyListbox}"><ListBox.ItemTemplate><DataTemplate><Grid><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition Width="4*"/></Grid.ColumnDefinitions><TextBlock Text="{Binding Address}" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center"/><Border BorderThickness="0,0,0,1" BorderBrush="Black"/><ListBox Grid.Column="1" ItemsSource="{Binding Items}" Style="{StaticResource ItemsListBoxStyle}"/></Grid></DataTemplate></ListBox.ItemTemplate>
</ListBox>
2、結果在鼠標滾輪時不觸發外層的滑動效果:
3、方法①:設置IsHitTestVisible,結果內部控件沒辦法選擇:
<ListBox Grid.Column="1" AlternationCount="2"ItemsSource="{Binding Items}" IsHitTestVisible="False"Style="{StaticResource ItemsListBoxStyle}"/>
4、方法②通過事件的方式處理:
<ListBox Grid.Column="1" AlternationCount="2"PreviewMouseWheel="ManufacturerListBox_PreviewMouseWheel"ItemsSource="{Binding?Items}"?Style="{StaticResource?ItemsListBoxStyle}"/>
5、后臺事件邏輯:
var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
eventArg.RoutedEvent = UIElement.MouseWheelEvent;
eventArg.Source = sender;
var parent = ((Control) sender).Parent as UIElement;
parent.RaiseEvent(eventArg);
6、方法③很明顯前三種都不滿足需求;最后突然發現:截然內部滑動讓他失效,換句話說就是不要他了;結果很簡單的方式:
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="0" SnapsToDevicePixels="true"><!--3、方法:比較機智的方法既然里面的ScrollViewer會影響外層滑輪事件,那咱就直接把他干掉得了--><!--<ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}">--><ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" IsHitTestVisible="True"/><!--</ScrollViewer>-->
</Border>
最終簡單的效果先這樣吧;以后有時間的話,可以再去摸索一下更復雜的效果
;編程不息、Bug不止、無Bug、無生活
;改bug的冷靜、編碼的激情、完成后的喜悅、挖坑的激動 、填坑的興奮;這也許就是屌絲程序員的樂趣吧;今天就到這里吧;希望自己有動力一步一步堅持下去;生命不息,代碼不止;大家抽空可以看看今天分享的效果,有好的意見和想法,可以在留言板隨意留言;我看到后會第一時間回復大家,多謝大家的一直默默的關注和支持!如果覺得不錯,那就伸出您的小手點個贊并關注一下!