|
Partner
|
|
|
FolderBrowseDialog in WPF
Autor:
TKundNobody2
|
Sprache:
C#
|
Bewertung:
noch nicht bewertet
|
Anzahl der Aufrufe:
7753
|
Beschreibung:
WPF hat leider keinen FolderBrowseDialog mehr wie in WinForms. Daher habe ich angefangen diesen nach zubauen. Ich habe jetzt die Version 0.1.0 veröffentlicht (http://wpfdialogs.codeplex.com/), wo der FolderBrowseDialog ein DialogResult hat (wie in WinForms nicht wie WPF-Dialoge) und die Möglichkeit besteht den ausgewählten Pfad auszulesen. Also einfach die Grundfunktionen bereitstellt
IDialog.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace WPF_Dialogs { /// /// This Interface implements general Dialogfeatures: enum with Dialogresults /// public interface IDialog { EDialogResult DialogResult { get; set; } }
/// /// Represents all possible DialogResults (same as in the System.Windows.Forms-namespace) /// public enum EDialogResult : int { Abort = 1, Cancel = 2, Ignore = 3, No = 4, None = 5, OK = 6, Retry = 7, Yes = 8 } }
Custom Controls\DialogButton.xaml
< Button x:Class="WPF_Dialogs.Custom_Controls.DialogButton" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" Height="23" Width="75">
< /Button>
Custom Controls\DialogButton.xaml.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes;
using WPF_Dialogs;
namespace WPF_Dialogs.Custom_Controls { /// /// Interaction logic for DialogButton.xaml /// public partial class DialogButton : Button, IDialog { /// /// Gets or sets the dialog result. DEFAULT: None /// /// The dialog result. public EDialogResult DialogResult { get; set; }
public WPF_Dialogs.Dialogs.FolderBrowseDialog FolderBrowseDialog { get { throw new System.NotImplementedException(); } set { } }
public DialogButton() { InitializeComponent(); this.Content = "DialogButton"; this.DialogResult = EDialogResult.None; this.Height = 23; this.Width = 75; } } }
FolderBrowseDialog.xaml XAML-Code für das Code-Snippet
< Window x:Class="WPF_Dialogs.Dialogs.FolderBrowseDialog" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:custom="clr-namespace:WPF_Dialogs.Custom_Controls" Title="Open Folder" Height="300" Width="300" ResizeMode="NoResize"> < Grid> < custom:DialogButton Margin="102,226,102,12" Content="OK" DialogResult="OK" Click="DialogButton_clicked" /> < custom:DialogButton Margin="181,226,22,12" Content="Cancel" DialogResult="Cancel" Click="DialogButton_clicked" /> < Canvas Height="208" HorizontalAlignment="Left" Margin="12,12,0,0" Name="canvas1" VerticalAlignment="Top" Width="254"> < TreeView Canvas.Left="0" Canvas.Top="0" Height="208" Name="treeViewFolders" Width="254" /> < /Canvas> < /Grid> < /Window>
Abgelegt unter: Dialog, WPF, FolderBrowseDialog.
|
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WPF_Dialogs; // Dadurch kann direkt auf IDialog und EDialogResult zugegriffen werden
using WPF_Dialogs.Custom_Controls; //Beinhaltet bisher nur den DialogButton
namespace WPF_Dialogs.Dialogs
{
public partial class FolderBrowseDialog : Window, IDialog
{
public new EDialogResult DialogResult { set; get; }
public string SelectedPath { get; private set; }
public FolderBrowseDialog()
{
InitializeComponent();
loadFolders();
}
private void DialogButton_clicked(object sender, RoutedEventArgs e)
{
DialogButton o = (DialogButton)sender;
this.DialogResult = o.DialogResult;
if (this.DialogResult == EDialogResult.OK && this.treeViewFolders.SelectedItem != null)
this.SelectedPath = ((TreeViewItem)this.treeViewFolders.SelectedItem).ToolTip.ToString();
this.Close();
}
/// <summary>
/// Zeigt den Dialog an. Kann auch so verwendet werden als ob es keinen Rückgabetyp hätte
/// </summary>
/// <returns></returns>
public EDialogResult showDialog()
{
this.ShowDialog();
return this.DialogResult;
}
private void loadFolders() //Laden der Ordner beim initialisieren
{
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); //Liest den Pfad zum Desktop
treeViewFolders.Items.Add(loadDirectories(desktop));
((TreeViewItem)treeViewFolders.Items[0]).IsExpanded = true;
}
private TreeViewItem loadDirectories(string path) //lädt alle Dateien vom path (nur dekstop) aus
{
TreeViewItem tvi = new TreeViewItem();
string[] folders = Directory.GetLogicalDrives();
tvi.Header = path.Remove(0, path.LastIndexOf("\\") + 1);
tvi.ToolTip = path;
tvi.Items.Add(createTreeVieItem(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)));
for (int i = 0; i < folders.Length; i++) //Ausgeben der verfügbaren laufwerk(e/sbuchstaben)
{
try
{
tvi.Items.Add(createTreeVieItem(folders[i]));
tvi.Expanded += new RoutedEventHandler(tvi_Expanded);
}
catch (Exception e) //The Drive is not ready, bad solution but best now
{ }
}
folders = Directory.GetDirectories(path);
for (int i = 0; i < folders.Length; i++) //ausgeben der Ordner
{
tvi.Items.Add(createTreeVieItem(folders[i]));
tvi.Expanded +=new RoutedEventHandler(tvi_Expanded);
}
return tvi;
}
private TreeViewItem createTreeVieItem(string path) //hilfsfunktion zum erstellen der treeviewitems
{
TreeViewItem tvi = new TreeViewItem();
string header = path.Remove(0, path.LastIndexOf("\\") + 1);
if (header == "") //The Path is a Drive (e.g. C\, after removing \ -> "")
{
header = path.Remove(path.Length - 1);
}
tvi.Header = header;
tvi.ToolTip = path;
if (path != "")
{
if (Directory.GetDirectories(path).Length > 0)
tvi.Items.Add(new TreeViewItem());
}
return tvi;
}
private void tvi_Expanded(object sender, RoutedEventArgs e) //nachladen der unterordner beim expandieren
{
var tvi = e.OriginalSource as TreeViewItem;
if (tvi.Items.Count <= 1 && tvi.IsExpanded)
{
tvi.Items.Clear();
string[] folders = Directory.GetDirectories(tvi.ToolTip.ToString());
for (int i = 0; i < folders.Length; i++)
{
try
{
tvi.Items.Add(createTreeVieItem(folders[i]));
tvi.Expanded += new RoutedEventHandler(tvi_Expanded);
}
catch (Exception ex) //If there are no rights for the folder, bad solution but best now
{ }
}
}
}
}
}
|
|
Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)
|
TKundNobody2 schrieb am:
24.02.2010 11:00:09
|
TKundNobody2 schrieb am:
24.02.2010 14:48:35
|
TKundNobody2 schrieb am:
02.03.2010 19:42:35
|
|
Diese Snippets könnten für Sie interessant sein:
|
|
|
|
|
|
|
|
|