Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit 383f5c5

Browse files
committed
Support folder selection in FilePickerTextBox
1 parent 2e6f8bd commit 383f5c5

File tree

2 files changed

+84
-9
lines changed

2 files changed

+84
-9
lines changed

OnnxStack.UI/UserControls/FilePickerTextBox.xaml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,40 @@
4040
<MultiDataTrigger.Conditions>
4141
<Condition Binding="{Binding IsEnabled, ElementName=UI}" Value="True"/>
4242
<Condition Binding="{Binding IsRequired}" Value="True"/>
43+
<Condition Binding="{Binding IsFolderPicker}" Value="False"/>
4344
</MultiDataTrigger.Conditions>
4445
<Setter Property="Text">
4546
<Setter.Value>
4647
<Binding Path="FileName" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True">
4748
<Binding.ValidationRules>
48-
<local:FileExistsValidationRule IsRequired="True" ValidatesOnTargetUpdated="True" />
49+
<local:FileExistsValidationRule IsRequired="True" IsFolder="False" ValidatesOnTargetUpdated="True" />
4950
</Binding.ValidationRules>
5051
</Binding>
5152
</Setter.Value>
5253
</Setter>
5354
</MultiDataTrigger>
55+
<MultiDataTrigger>
56+
<MultiDataTrigger.Conditions>
57+
<Condition Binding="{Binding IsEnabled, ElementName=UI}" Value="True"/>
58+
<Condition Binding="{Binding IsRequired}" Value="True"/>
59+
<Condition Binding="{Binding IsFolderPicker}" Value="True"/>
60+
</MultiDataTrigger.Conditions>
61+
<Setter Property="Text">
62+
<Setter.Value>
63+
<Binding Path="FileName" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True">
64+
<Binding.ValidationRules>
65+
<local:FileExistsValidationRule IsRequired="True" IsFolder="True" ValidatesOnTargetUpdated="True" />
66+
</Binding.ValidationRules>
67+
</Binding>
68+
</Setter.Value>
69+
</Setter>
70+
</MultiDataTrigger>
71+
<MultiDataTrigger>
72+
<MultiDataTrigger.Conditions>
73+
<Condition Binding="{Binding IsRequired}" Value="False"/>
74+
</MultiDataTrigger.Conditions>
75+
<Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
76+
</MultiDataTrigger>
5477
</Style.Triggers>
5578
</Style>
5679
</TextBox.Style>

OnnxStack.UI/UserControls/FilePickerTextBox.xaml.cs

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public partial class FilePickerTextBox : UserControl
1515
public FilePickerTextBox()
1616
{
1717
ClearFileCommand = new RelayCommand(ClearFile, CanExecuteClearFile);
18-
OpenFileDialogCommand = new RelayCommand(OpenFilePicker, CanExecuteOpenFilePicker);
18+
OpenFileDialogCommand = new RelayCommand(OpenPicker, CanExecuteOpenPicker);
1919
InitializeComponent();
2020
}
2121

@@ -37,6 +37,9 @@ public FilePickerTextBox()
3737
public static readonly DependencyProperty IsRequiredProperty =
3838
DependencyProperty.Register("IsRequired", typeof(bool), typeof(FilePickerTextBox));
3939

40+
public static readonly DependencyProperty IsFolderPickerProperty =
41+
DependencyProperty.Register("IsFolderPicker", typeof(bool), typeof(FilePickerTextBox));
42+
4043

4144
/// <summary>
4245
/// Gets or sets the clear file command.
@@ -115,6 +118,46 @@ public bool IsRequired
115118
}
116119

117120

121+
/// <summary>
122+
/// Gets or sets a value indicating whether this instance is folder picker.
123+
/// </summary>
124+
/// <value>
125+
/// <c>true</c> if this instance is folder picker; otherwise, <c>false</c>.
126+
/// </value>
127+
public bool IsFolderPicker
128+
{
129+
get { return (bool)GetValue(IsFolderPickerProperty); }
130+
set { SetValue(IsFolderPickerProperty, value); }
131+
}
132+
133+
134+
/// <summary>
135+
/// Opens the picker.
136+
/// </summary>
137+
private void OpenPicker()
138+
{
139+
if (IsFolderPicker)
140+
{
141+
OpenFolderPicker();
142+
return;
143+
}
144+
145+
OpenFilePicker();
146+
}
147+
148+
149+
/// <summary>
150+
/// Determines whether this instance can execute OpenPicker.
151+
/// </summary>
152+
/// <returns>
153+
/// <c>true</c> if this instance can execute OpenPicker; otherwise, <c>false</c>.
154+
/// </returns>
155+
private bool CanExecuteOpenPicker()
156+
{
157+
return true;
158+
}
159+
160+
118161
/// <summary>
119162
/// Opens the file picker.
120163
/// </summary>
@@ -135,14 +178,19 @@ private void OpenFilePicker()
135178

136179

137180
/// <summary>
138-
/// Determines whether this instance can execute OpenFilePicker.
181+
/// Opens the folder picker.
139182
/// </summary>
140-
/// <returns>
141-
/// <c>true</c> if this instance can execute OpenFilePicker; otherwise, <c>false</c>.
142-
/// </returns>
143-
private bool CanExecuteOpenFilePicker()
183+
private void OpenFolderPicker()
144184
{
145-
return true;
185+
var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog
186+
{
187+
Description = Title,
188+
InitialDirectory = InitialDirectory,
189+
UseDescriptionForTitle = true
190+
};
191+
var dialogResult = folderBrowserDialog.ShowDialog();
192+
if (dialogResult == System.Windows.Forms.DialogResult.OK)
193+
FileName = folderBrowserDialog.SelectedPath;
146194
}
147195

148196

@@ -174,6 +222,7 @@ private bool CanExecuteClearFile()
174222
/// <seealso cref="System.Windows.Controls.ValidationRule" />
175223
public class FileExistsValidationRule : ValidationRule
176224
{
225+
public bool IsFolder { get; set; }
177226
public bool IsRequired { get; set; }
178227

179228
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
@@ -182,9 +231,12 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
182231
if (!IsRequired && string.IsNullOrEmpty(filename))
183232
return ValidationResult.ValidResult;
184233

185-
if (!File.Exists(filename))
234+
if (!IsFolder && !File.Exists(filename))
186235
return new ValidationResult(false, $"File does not exist");
187236

237+
if (IsFolder && !Directory.Exists(filename))
238+
return new ValidationResult(false, $"Directory does not exist");
239+
188240
return ValidationResult.ValidResult;
189241
}
190242
}

0 commit comments

Comments
 (0)