码迷,mamicode.com
首页 > 移动开发 > 详细

WPf 带滚动条WrapPanel 自动换行 和控件右键菜单

时间:2019-12-20 23:54:57      阅读:492      评论:0      收藏:0      [点我收藏+]

标签:pre   component   自动   hidden   panel   滚动   head   ram   err   

原文:WPf 带滚动条WrapPanel 自动换行 和控件右键菜单

技能点包括 WPf 样式的引用 数据的验证和绑定 比较适合初学者

技术图片

前台:

  1. <Window.Resources>
  2. <local:PathToSource x:Key="n2"/>
  3. <Style x:Key="{x:Type ContextMenu}" TargetType="{x:Type ContextMenu}">
  4. <Setter Property="OverridesDefaultStyle" Value="True"/>
  5. <Setter Property="SnapsToDevicePixels" Value="True"/>
  6. <Setter Property="Template">
  7. <Setter.Value>
  8. <ControlTemplate TargetType="{x:Type ContextMenu}">
  9. <Border Background="#CD222120" CornerRadius="7, 7, 8, 8" BorderBrush="DarkGray" BorderThickness="2" Opacity="0.96">
  10. <StackPanel ClipToBounds="True" Orientation="Vertical" IsItemsHost="True" Margin="5,4,5,4"/>
  11. </Border>
  12. </ControlTemplate>
  13. </Setter.Value>
  14. </Setter>
  15. </Style>
  16. <ControlTemplate x:Key="{x:Static MenuItem.TopLevelItemTemplateKey}" TargetType="{x:Type MenuItem}">
  17. <Border Name="Border" >
  18. <Grid>
  19. <ContentPresenter Margin="6,3,6,3" ContentSource="Header" RecognizesAccessKey="True" />
  20. </Grid>
  21. </Border>
  22. </ControlTemplate>
  23. <DataTemplate x:Key="Wrapitem">
  24. <Grid Height="86" Width="86" >
  25. <Grid.ContextMenu>
  26. <ContextMenu Name="cm" StaysOpen="true">
  27. <MenuItem Header="update"/>
  28. <MenuItem Header="Save"/>
  29. <MenuItem Header="SaveAs"/>
  30. </ContextMenu>
  31. </Grid.ContextMenu>
  32. <Image Margin="8,8,8,24" Source="{Binding Path=Url,Converter={StaticResource n2}}"/>
  33. <Label Content="{Binding Path=Text}" Height="22" Width="45" VerticalAlignment="Bottom"/>
  34. </Grid>
  35. </DataTemplate>
  36. </Window.Resources>
  37. <Grid>
  38. <ListBox x:Name="list" Margin="124,63,109,44" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ItemTemplate="{StaticResource Wrapitem}">
  39. <ListBox.Resources>
  40. <Style TargetType="ListBoxItem">
  41. <Style.Resources>
  42. <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FFA1A1A1"/>
  43. </Style.Resources>
  44. </Style>
  45. </ListBox.Resources>
  46. <!--<ListBox.ItemsPanel >
  47. <ItemsPanelTemplate>
  48. <WrapPanel Orientation="Horizontal" IsItemsHost="True"/>
  49. </ItemsPanelTemplate>
  50. </ListBox.ItemsPanel>-->
  51. <ListBox.Template>
  52. <ControlTemplate TargetType="{x:Type ListBox}">
  53. <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
  54. <WrapPanel Orientation="Horizontal" IsItemsHost="True" ScrollViewer.CanContentScroll="True"/>
  55. </ScrollViewer>
  56. </ControlTemplate>
  57. </ListBox.Template>
  58. </ListBox>
  59. </Grid>
  60. </Window>

后台:

  1. /// <summary>
  2. /// MainWindow.xaml 的交互逻辑
  3. /// </summary>
  4. public partial class MainWindow : Window
  5. {
  6. public MainWindow()
  7. {
  8. InitializeComponent();
  9. load();
  10. }
  11. public void load()
  12. {
  13. ObservableCollection<Mean> ListMean = new ObservableCollection<Mean>()
  14. {
  15. new Mean(){Text="001",Url="i"},
  16. new Mean(){Text="002",Url="i"},
  17. new Mean(){Text="003",Url="ii"},
  18. new Mean(){Text="004",Url="i"},
  19. new Mean(){Text="005",Url="i"},
  20. new Mean(){Text="006",Url="ii"},
  21. new Mean(){Text="007",Url="i"},
  22. new Mean(){Text="008",Url="i"},
  23. new Mean(){Text="009",Url="i"},
  24. new Mean(){Text="010",Url="i"},
  25. new Mean(){Text="011",Url="ii"},
  26. new Mean(){Text="012",Url="i"},
  27. new Mean(){Text="013",Url="i"},
  28. new Mean(){Text="014",Url="i"},
  29. new Mean(){Text="015",Url="i"},
  30. new Mean(){Text="016",Url="i"},
  31. new Mean(){Text="017",Url="i"}
  32. };
  33. list.ItemsSource = ListMean;
  34. }
  35. }
  36. public class Mean : INotifyPropertyChanged
  37. {
  38. private string text;
  39. public string Text
  40. {
  41. get { return text; }
  42. set { text = value; OnPropertyChanged("Text"); }
  43. }
  44. private string url;
  45. public string Url
  46. {
  47. get { return url; }
  48. set { url = value; OnPropertyChanged("Url"); }
  49. }
  50. public event PropertyChangedEventHandler PropertyChanged;
  51. public void OnPropertyChanged(string propertyName)
  52. {
  53. if (PropertyChanged != null)
  54. {
  55. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  56. }
  57. }
  58. }
  59. public class PathToSource:IValueConverter
  60. {
  61. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  62. {
  63. string url = string.Format(@"/Images/{0}.jpg", (string)value=="i"?"i":"ii");
  64. return new BitmapImage(new Uri(url,UriKind.Relative));
  65. }
  66. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  67. {
  68. throw new NotImplementedException();
  69. }
  70. }


WPf 带滚动条WrapPanel 自动换行 和控件右键菜单

标签:pre   component   自动   hidden   panel   滚动   head   ram   err   

原文地址:https://www.cnblogs.com/lonelyxmas/p/12075376.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!