加入MaterialDesign
App.xaml
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- This is the current way to setup your app's initial theme -->
<materialDesign:BundledTheme BaseTheme="Inherit" PrimaryColor="Teal" SecondaryColor="Lime" ColorAdjustment="{materialDesign:ColorAdjustment}" />
<!-- If you would prefer to use your own colors there is an option for that as well -->
<!--<materialDesign:CustomColorTheme BaseTheme="Light" PrimaryColor="Aqua" SecondaryColor="DarkGreen" />-->
<!-- You can also use the built in theme dictionaries as well
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
-->
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
MianWindow.xaml
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
图片列表容器
MianWindow.xaml
<materialDesign:DialogHost Identifier="FileDialog" DialogTheme="Inherit" SnackbarMessageQueue="{Binding ElementName=MainSnackbar, Path=MessageQueue}" DialogOpened="FileDialog_DialogOpened" >
<Grid Name="xxxx">
<!--设置listBox item容器-->
<ScrollViewer Margin="0,120,0,35" >
<domain:CustomPanel x:Name="ImgsPanel" ColumnCount="5" Loaded="ImgsPanel_Loaded" >
</domain:CustomPanel>
</ScrollViewer>
</Grid>
</materialDesign:DialogHost>
载入图片列表
MainWindow.xaml.cs
private void Button_Click_1(object sender, RoutedEventArgs e)
{
DirectoryInfo dir = new DirectoryInfo("C:\\Users\\frees\\Desktop\\Demo\\img");//图片集路径
try
{
if (!dir.Exists)//判断所指的文件夹/文件是否存在
return;
FileSystemInfo[] files = dir.GetFileSystemInfos();//获取文件夹下所有文件和文件夹
int i = 0;
foreach (FileSystemInfo FSys in files)
{
FileInfo fileInfo = FSys as FileInfo;
if (fileInfo != null)
{
FileInfo SFInfo = new FileInfo(fileInfo.DirectoryName + "\\" + fileInfo.Name);
string ex = SFInfo.Extension.ToLower();
//Console.WriteLine("Name" + SFInfo.Name+ " FullName" + SFInfo.FullName + " GetFileNameWithoutExtension" + System.IO.Path.GetFileNameWithoutExtension(SFInfo.FullName));
string sName = System.IO.Path.GetFileNameWithoutExtension(SFInfo.FullName);
if (ex == ".jpg" || ex == ".png" || ex == ".jpeg" || ex == ".gif")
{
CreateGard(SFInfo.FullName,sName, i.ToString());
}
}
i++;
}
}
catch (Exception ex)
{
MessageBox.Show("错误信息:" + ex.Message, "打开文件错误", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.ServiceNotification);
}
}
/**
* 创建一个图片
*/
private void CreateGard(string PicPath,string Info,string id)
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(PicPath, UriKind.RelativeOrAbsolute);
bi.EndInit();
bi.Freeze();
Image image = new Image();
image.Source = bi;
image.Stretch = Stretch.Uniform;
image.Width = 190;
image.Margin = new Thickness(5, 5, 5, 32);
TextBlock tb = new TextBlock();
tb.FontWeight = SystemFonts.MenuFontWeight;
tb.VerticalAlignment = VerticalAlignment.Bottom;
tb.Margin = new Thickness(5, 5, 0, 5);
tb.Text = Info;
Button but = new Button();
but.VerticalAlignment = VerticalAlignment.Bottom;
but.HorizontalAlignment = HorizontalAlignment.Right;
but.Margin = new Thickness(0, 0, 8, 4);
Style btn_Orange = (Style)this.FindResource("MaterialDesignFloatingActionMiniAccentButton");
but.Style = btn_Orange;
var packIcon = new PackIcon
{
Kind = PackIconKind.Download,
};
but.Content = packIcon;
but.Name = "but_" + id;
but.Click += new RoutedEventHandler(btnEvent_Click);//给动态按钮绑定点击事件
Grid grid1 = new Grid();
grid1.Width = 200;
grid1.Children.Add(tb);
grid1.Children.Add(image);
grid1.Children.Add(but);
Card car = new Card();
car.Margin = new Thickness(5, 5, 5, 5);
car.Content = grid1;
ImgsPanel.Children.Add(car);
}
//动态按钮单击的事件
private void btnEvent_Click(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
MessageBox.Show("点击动态按钮:" + button.Name, "提示", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.ServiceNotification);
//Application.Current.Shutdown();
}
深色模式开关
// MaterialDesignThemes 深色模式开关
private void TbutDark_Click(object sender, RoutedEventArgs e)
=> ModifyTheme(TbutDark.IsChecked == true);
private static void ModifyTheme(bool isDarkTheme)
{
var paletteHelper = new PaletteHelper();
var theme = paletteHelper.GetTheme();
theme.SetBaseTheme(isDarkTheme ? Theme.Dark : Theme.Light);
paletteHelper.SetTheme(theme);
}
完整Demo下载