当前位置:   article > 正文

C#+WPF+MaterialDesign图片瀑布流 和 一键深色模式_wpf 图片瀑布流

wpf 图片瀑布流

加入MaterialDesign

App.xaml

  1. <ResourceDictionary>
  2. <ResourceDictionary.MergedDictionaries>
  3. <!-- This is the current way to setup your app's initial theme -->
  4. <materialDesign:BundledTheme BaseTheme="Inherit" PrimaryColor="Teal" SecondaryColor="Lime" ColorAdjustment="{materialDesign:ColorAdjustment}" />
  5. <!-- If you would prefer to use your own colors there is an option for that as well -->
  6. <!--<materialDesign:CustomColorTheme BaseTheme="Light" PrimaryColor="Aqua" SecondaryColor="DarkGreen" />-->
  7. <!-- You can also use the built in theme dictionaries as well
  8. <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
  9. <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
  10. <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
  11. -->
  12. <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
  13. <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
  14. </ResourceDictionary.MergedDictionaries>
  15. </ResourceDictionary>

MianWindow.xaml

xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"

图片列表容器

MianWindow.xaml

  1. <materialDesign:DialogHost Identifier="FileDialog" DialogTheme="Inherit" SnackbarMessageQueue="{Binding ElementName=MainSnackbar, Path=MessageQueue}" DialogOpened="FileDialog_DialogOpened" >
  2. <Grid Name="xxxx">
  3. <!--设置listBox item容器-->
  4. <ScrollViewer Margin="0,120,0,35" >
  5. <domain:CustomPanel x:Name="ImgsPanel" ColumnCount="5" Loaded="ImgsPanel_Loaded" >
  6. </domain:CustomPanel>
  7. </ScrollViewer>
  8. </Grid>
  9. </materialDesign:DialogHost>

载入图片列表

MainWindow.xaml.cs

  1. private void Button_Click_1(object sender, RoutedEventArgs e)
  2. {
  3. DirectoryInfo dir = new DirectoryInfo("C:\\Users\\frees\\Desktop\\Demo\\img");//图片集路径
  4. try
  5. {
  6. if (!dir.Exists)//判断所指的文件夹/文件是否存在
  7. return;
  8. FileSystemInfo[] files = dir.GetFileSystemInfos();//获取文件夹下所有文件和文件夹
  9. int i = 0;
  10. foreach (FileSystemInfo FSys in files)
  11. {
  12. FileInfo fileInfo = FSys as FileInfo;
  13. if (fileInfo != null)
  14. {
  15. FileInfo SFInfo = new FileInfo(fileInfo.DirectoryName + "\\" + fileInfo.Name);
  16. string ex = SFInfo.Extension.ToLower();
  17. //Console.WriteLine("Name" + SFInfo.Name+ " FullName" + SFInfo.FullName + " GetFileNameWithoutExtension" + System.IO.Path.GetFileNameWithoutExtension(SFInfo.FullName));
  18. string sName = System.IO.Path.GetFileNameWithoutExtension(SFInfo.FullName);
  19. if (ex == ".jpg" || ex == ".png" || ex == ".jpeg" || ex == ".gif")
  20. {
  21. CreateGard(SFInfo.FullName,sName, i.ToString());
  22. }
  23. }
  24. i++;
  25. }
  26. }
  27. catch (Exception ex)
  28. {
  29. MessageBox.Show("错误信息:" + ex.Message, "打开文件错误", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.ServiceNotification);
  30. }
  31. }
  32. /**
  33. * 创建一个图片
  34. */
  35. private void CreateGard(string PicPath,string Info,string id)
  36. {
  37. BitmapImage bi = new BitmapImage();
  38. bi.BeginInit();
  39. bi.UriSource = new Uri(PicPath, UriKind.RelativeOrAbsolute);
  40. bi.EndInit();
  41. bi.Freeze();
  42. Image image = new Image();
  43. image.Source = bi;
  44. image.Stretch = Stretch.Uniform;
  45. image.Width = 190;
  46. image.Margin = new Thickness(5, 5, 5, 32);
  47. TextBlock tb = new TextBlock();
  48. tb.FontWeight = SystemFonts.MenuFontWeight;
  49. tb.VerticalAlignment = VerticalAlignment.Bottom;
  50. tb.Margin = new Thickness(5, 5, 0, 5);
  51. tb.Text = Info;
  52. Button but = new Button();
  53. but.VerticalAlignment = VerticalAlignment.Bottom;
  54. but.HorizontalAlignment = HorizontalAlignment.Right;
  55. but.Margin = new Thickness(0, 0, 8, 4);
  56. Style btn_Orange = (Style)this.FindResource("MaterialDesignFloatingActionMiniAccentButton");
  57. but.Style = btn_Orange;
  58. var packIcon = new PackIcon
  59. {
  60. Kind = PackIconKind.Download,
  61. };
  62. but.Content = packIcon;
  63. but.Name = "but_" + id;
  64. but.Click += new RoutedEventHandler(btnEvent_Click);//给动态按钮绑定点击事件
  65. Grid grid1 = new Grid();
  66. grid1.Width = 200;
  67. grid1.Children.Add(tb);
  68. grid1.Children.Add(image);
  69. grid1.Children.Add(but);
  70. Card car = new Card();
  71. car.Margin = new Thickness(5, 5, 5, 5);
  72. car.Content = grid1;
  73. ImgsPanel.Children.Add(car);
  74. }
  75. //动态按钮单击的事件
  76. private void btnEvent_Click(object sender, RoutedEventArgs e)
  77. {
  78. Button button = (Button)sender;
  79. MessageBox.Show("点击动态按钮:" + button.Name, "提示", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.ServiceNotification);
  80. //Application.Current.Shutdown();
  81. }

深色模式开关

  1. // MaterialDesignThemes 深色模式开关
  2. private void TbutDark_Click(object sender, RoutedEventArgs e)
  3. => ModifyTheme(TbutDark.IsChecked == true);
  4. private static void ModifyTheme(bool isDarkTheme)
  5. {
  6. var paletteHelper = new PaletteHelper();
  7. var theme = paletteHelper.GetTheme();
  8. theme.SetBaseTheme(isDarkTheme ? Theme.Dark : Theme.Light);
  9. paletteHelper.SetTheme(theme);
  10. }

完整Demo下载

https://download.csdn.net/download/fgg110/86505897

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/97831
推荐阅读
相关标签
  

闽ICP备14008679号