Skip to content

Commit

Permalink
Add WPF OpenFolderDialog and OpenFileDialog previews for WPF UI d…
Browse files Browse the repository at this point in the history
  • Loading branch information
pomianowski committed Sep 6, 2023
1 parent 9bfe472 commit b5fe3f3
Show file tree
Hide file tree
Showing 6 changed files with 401 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/Wpf.Ui.Gallery/Controls/ControlExample.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

<Border
Grid.Row="1"
Padding="10"
Padding="16"
Background="{ui:ThemeResource CardStrokeColorDefaultBrush}"
BorderBrush="{ui:ThemeResource ControlElevationBorderBrush}"
BorderThickness="1,1,1,0"
Expand Down Expand Up @@ -71,7 +71,7 @@
<ui:TextBlock
Margin="0,0,0,5"
FontTypography="BodyStrong"
Text="CSharp" />
Text="C#" />

<syntax:CodeBlock
Padding="0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,52 @@

namespace Wpf.Ui.Gallery.ViewModels.Pages.OpSystem;

public partial class ClipboardViewModel : ObservableObject { }
public partial class ClipboardViewModel : ObservableObject
{
[ObservableProperty]
private string _textToCopy = "This text will be copied to the clipboard.";

[ObservableProperty]
private string _clipboardContent = "Click the button!";

[ObservableProperty]
private Visibility _textCopiedVisibility = Visibility.Collapsed;

[RelayCommand]
private async Task OnCopyTextToClipboard()
{
try
{
Clipboard.Clear();
Clipboard.SetText(TextToCopy);
}
catch (Exception e)
{
Debug.WriteLine(e);
}

if (TextCopiedVisibility == Visibility.Visible)
{
return;
}

TextCopiedVisibility = Visibility.Visible;

await Task.Delay(5000);

TextCopiedVisibility = Visibility.Collapsed;
}

[RelayCommand]
private void OnParseTextFromClipboard()
{
try
{
ClipboardContent = Clipboard.GetText();
}
catch (Exception e)
{
Debug.WriteLine(e);
}
}
}
137 changes: 136 additions & 1 deletion src/Wpf.Ui.Gallery/ViewModels/Pages/OpSystem/FilePickerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,141 @@
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using Microsoft.Win32;

namespace Wpf.Ui.Gallery.ViewModels.Pages.OpSystem;

public partial class FilePickerViewModel : ObservableObject { }
public partial class FilePickerViewModel : ObservableObject
{
[ObservableProperty]
private Visibility _openedFilePathVisibility = Visibility.Collapsed;

[ObservableProperty]
private string _openedFilePath = String.Empty;

[ObservableProperty]
private Visibility _openedPicturePathVisibility = Visibility.Collapsed;

[ObservableProperty]
private string _openedPicturePath = String.Empty;

[ObservableProperty]
private Visibility _openedMultiplePathVisibility = Visibility.Collapsed;

[ObservableProperty]
private string _openedMultiplePath = String.Empty;

[ObservableProperty]
private Visibility _openedFolderPathVisibility = Visibility.Collapsed;

[ObservableProperty]
private string _openedFolderPath = String.Empty;

[RelayCommand]
public void OnOpenFile()
{
OpenedFilePathVisibility = Visibility.Collapsed;

OpenFileDialog openFileDialog = new()
{
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
Filter = "All files (*.*)|*.*"
};

if (openFileDialog.ShowDialog() != true)
{
return;
}

if (!File.Exists(openFileDialog.FileName))
{
return;
}

OpenedFilePath = openFileDialog.FileName;
OpenedFilePathVisibility = Visibility.Visible;
}

[RelayCommand]
public void OnOpenPicture()
{
OpenedPicturePathVisibility = Visibility.Collapsed;

OpenFileDialog openFileDialog = new()
{
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
Filter = "Image files (*.bmp;*.jpg;*.jpeg;*.png)|*.bmp;*.jpg;*.jpeg;*.png|All files (*.*)|*.*"
};

if (openFileDialog.ShowDialog() != true)
{
return;
}

if (!File.Exists(openFileDialog.FileName))
{
return;
}

OpenedPicturePath = openFileDialog.FileName;
OpenedPicturePathVisibility = Visibility.Visible;
}

[RelayCommand]
public void OnOpenMultiple()
{
OpenedMultiplePathVisibility = Visibility.Collapsed;

OpenFileDialog openFileDialog = new()
{
Multiselect = true,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
Filter = "All files (*.*)|*.*"
};

if (openFileDialog.ShowDialog() != true)
{
return;
}

if (openFileDialog.FileNames.Length == 0)
{
return;
}

var fileNames = openFileDialog.FileNames;

OpenedMultiplePath = String.Join("\n", fileNames);
OpenedMultiplePathVisibility = Visibility.Visible;
}

[RelayCommand]
public void OnOpenFolder()
{
#if NET8_0_OR_GREATER
OpenedFolderPathVisibility = Visibility.Collapsed;

OpenFolderDialog openFolderDialog = new OpenFolderDialog()
{
Multiselect = true,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
};

if (openFolderDialog.ShowDialog() != true)
{
return;
}

if (openFolderDialog.FileNames.Length == 0)
{
return;
}

OpenedFolderPath = String.Join("\n", openFolderDialog.FileNames);
OpenedFolderPathVisibility = Visibility.Visible;
#else
OpenedFolderPath = "OpenFolderDialog requires .NET 8 or newer";
OpenedFolderPathVisibility = Visibility.Visible;
#endif
}
}
57 changes: 56 additions & 1 deletion src/Wpf.Ui.Gallery/Views/Pages/OpSystem/ClipboardPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,60 @@
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
mc:Ignorable="d">

<Grid />
<StackPanel Margin="0,0,0,24">
<controls:ControlExample
Margin="0"
CsharpCode="Clipboard.Clear();\nClipboard.SetText(TextToCopy);"
HeaderText="Copy text to the clipboard">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Margin="0,0,0,16">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ui:Button
Grid.Column="0"
Command="{Binding ViewModel.CopyTextToClipboardCommand}"
Content="Copy Text to the Clipboard" />
<ui:TextBlock
Grid.Column="1"
Margin="8,0,0,0"
VerticalAlignment="Center"
Text="Text copied to clipboard!"
Visibility="{Binding ViewModel.TextCopiedVisibility}" />
</Grid>
<ui:TextBox
Grid.Row="1"
MinLines="4"
Text="{Binding ViewModel.TextToCopy}" />
</Grid>
</controls:ControlExample>

<controls:ControlExample
Margin="0,32,0,0"
CsharpCode="ClipboardContent = Clipboard.GetText();"
HeaderText="Paste text from the clipboard">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ui:Button
Grid.Row="0"
Margin="0,0,0,16"
Command="{Binding ViewModel.ParseTextFromClipboardCommand}"
Content="Paste Text from the Clipboard" />
<ui:TextBlock
Grid.Row="1"
Text="Clipboard:"
TextDecorations="Underline" />
<ui:TextBlock Grid.Row="2" Text="{Binding ViewModel.ClipboardContent}" />
</Grid>
</controls:ControlExample>
</StackPanel>
</Page>
108 changes: 107 additions & 1 deletion src/Wpf.Ui.Gallery/Views/Pages/OpSystem/FilePickerPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,111 @@
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
mc:Ignorable="d">

<Grid />
<StackPanel Margin="0,0,0,24">
<controls:ControlExample
Margin="0"
CsharpCode="OpenFileDialog openFileDialog = new();"
HeaderText="Pick a single file">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ui:Button
Grid.Column="0"
Command="{Binding ViewModel.OpenFileCommand}"
Content="Open a file" />
<StackPanel
Grid.Column="1"
Margin="16,0,0,0"
VerticalAlignment="Center"
Orientation="Horizontal"
Visibility="{Binding ViewModel.OpenedFilePathVisibility}">
<ui:TextBlock Text="Picked file:" />
<ui:TextBlock
Margin="4,0,0,0"
FontTypography="BodyStrong"
Text="{Binding ViewModel.OpenedFilePath}" />

</StackPanel>
</Grid>
</controls:ControlExample>

<controls:ControlExample
Margin="0,32,0,0"
CsharpCode="OpenFileDialog openFileDialog = new();"
HeaderText="Pick a specific file type">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ui:Button
Grid.Column="0"
Command="{Binding ViewModel.OpenPictureCommand}"
Content="Open a picture" />
<StackPanel
Grid.Column="1"
Margin="16,0,0,0"
VerticalAlignment="Center"
Orientation="Horizontal"
Visibility="{Binding ViewModel.OpenedPicturePathVisibility}">
<ui:TextBlock Text="Picked photo:" />
<ui:TextBlock
Margin="4,0,0,0"
FontTypography="BodyStrong"
Text="{Binding ViewModel.OpenedPicturePath}" />
</StackPanel>
</Grid>
</controls:ControlExample>

<controls:ControlExample
Margin="0,32,0,0"
CsharpCode="OpenFileDialog openFileDialog = new();"
HeaderText="Pick multiple files">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ui:Button
Grid.Column="0"
Command="{Binding ViewModel.OpenMultipleCommand}"
Content="Open multiple files" />
<StackPanel
Grid.Column="1"
Margin="16,0,0,0"
VerticalAlignment="Center"
Visibility="{Binding ViewModel.OpenedMultiplePathVisibility}">
<ui:TextBlock Text="Picked files:" />
<ui:TextBlock FontTypography="BodyStrong" Text="{Binding ViewModel.OpenedMultiplePath}" />
</StackPanel>
</Grid>
</controls:ControlExample>

<controls:ControlExample
Margin="0,32,0,0"
CsharpCode="OpenFileDialog openFileDialog = new();"
HeaderText="Pick a folder">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ui:Button
Grid.Column="0"
Command="{Binding ViewModel.OpenFolderCommand}"
Content="Open a folder" />
<StackPanel
Grid.Column="1"
Margin="16,0,0,0"
VerticalAlignment="Center"
Visibility="{Binding ViewModel.OpenedFolderPathVisibility}">
<ui:TextBlock Text="Picked folder:" />
<ui:TextBlock FontTypography="BodyStrong" Text="{Binding ViewModel.OpenedFolderPath}" />
</StackPanel>
</Grid>
</controls:ControlExample>

</StackPanel>
</Page>
Loading

0 comments on commit b5fe3f3

Please sign in to comment.