wpf选取文件夹的控件
2011-12-22 15:22
302 查看
在 《wpf揭秘》这本书里有类似的控件。
.cs文件
using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
namespace Chapter16
{
[System.Windows.Markup.ContentProperty("FileName")]
public partial class FileInputBox : UserControl
{
public FileInputBox()
{
InitializeComponent();
theTextBox.TextChanged += new TextChangedEventHandler(OnTextChanged);
}
private void theButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog d = new OpenFileDialog();
if (d.ShowDialog() == true) // Result could be true, false, or null
this.FileName = d.FileName;
}
public string FileName
{
get { return (string)GetValue(FileNameProperty); }
set { SetValue(FileNameProperty, value); }
}
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
e.Handled = true;
RoutedEventArgs args = new RoutedEventArgs(FileNameChangedEvent);
RaiseEvent(args);
}
public event RoutedEventHandler FileNameChanged
{
add { AddHandler(FileNameChangedEvent, value); }
remove { RemoveHandler(FileNameChangedEvent, value); }
}
public static readonly DependencyProperty FileNameProperty =
DependencyProperty.Register("FileName", typeof(string), typeof(FileInputBox));
public static readonly RoutedEvent FileNameChangedEvent =
EventManager.RegisterRoutedEvent("FileNameChanged",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(FileInputBox));
}
}
.xaml文件
<UserControl x:Class="Chapter16.FileInputBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="root">
<DockPanel>
<Button x:Name="theButton" DockPanel.Dock="Right" Click="theButton_Click">Browse...</Button>
<TextBox x:Name="theTextBox" MinWidth="{Binding ActualWidth, ElementName=theButton}" Text="{Binding FileName, ElementName=root}" Margin="0,0,2,0"/>
</DockPanel>
</UserControl>
这个控件可以直接拿来用。它是选择文件的,我们需要修改函数
private void theButton_Click(object sender, RoutedEventArgs e)
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
this.FileName = folderBrowserDialog.SelectedPath;
}
}
FolderBrowserDialog 是using System.Windows.Forms;的一个控件。
关于这dialog在/article/6028094.html有详细说明。
.cs文件
using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
namespace Chapter16
{
[System.Windows.Markup.ContentProperty("FileName")]
public partial class FileInputBox : UserControl
{
public FileInputBox()
{
InitializeComponent();
theTextBox.TextChanged += new TextChangedEventHandler(OnTextChanged);
}
private void theButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog d = new OpenFileDialog();
if (d.ShowDialog() == true) // Result could be true, false, or null
this.FileName = d.FileName;
}
public string FileName
{
get { return (string)GetValue(FileNameProperty); }
set { SetValue(FileNameProperty, value); }
}
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
e.Handled = true;
RoutedEventArgs args = new RoutedEventArgs(FileNameChangedEvent);
RaiseEvent(args);
}
public event RoutedEventHandler FileNameChanged
{
add { AddHandler(FileNameChangedEvent, value); }
remove { RemoveHandler(FileNameChangedEvent, value); }
}
public static readonly DependencyProperty FileNameProperty =
DependencyProperty.Register("FileName", typeof(string), typeof(FileInputBox));
public static readonly RoutedEvent FileNameChangedEvent =
EventManager.RegisterRoutedEvent("FileNameChanged",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(FileInputBox));
}
}
.xaml文件
<UserControl x:Class="Chapter16.FileInputBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="root">
<DockPanel>
<Button x:Name="theButton" DockPanel.Dock="Right" Click="theButton_Click">Browse...</Button>
<TextBox x:Name="theTextBox" MinWidth="{Binding ActualWidth, ElementName=theButton}" Text="{Binding FileName, ElementName=root}" Margin="0,0,2,0"/>
</DockPanel>
</UserControl>
这个控件可以直接拿来用。它是选择文件的,我们需要修改函数
private void theButton_Click(object sender, RoutedEventArgs e)
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
this.FileName = folderBrowserDialog.SelectedPath;
}
}
FolderBrowserDialog 是using System.Windows.Forms;的一个控件。
关于这dialog在/article/6028094.html有详细说明。
相关文章推荐
- wpf中动态更改控件或元素的样式
- Winform用户控件添加WPF用户控件方法
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(十四) 精灵控件横空出世!①
- Wpf 动态添加控件设置样式
- C# WPF Border控件总结
- WPF调用Winfrom控件
- C# 遍历文件夹图片并用ListView控件展示
- WPF:理解ContentControl——动态添加控件和查找控件
- WPF中一个控件绑定另一个控件的属性
- VC:遍历文件夹下所有文件和文件夹(CFileFind类、树视图控件、图像链表、HTREEITEM句柄、TV_INSERTSTRUCT 结构体)
- C# WPF 控件通用属性简介
- WPF:获取控件内的子项
- WPf 带滚动条WrapPanel 自动换行 和控件右键菜单
- WPF之复杂形状控件
- WPF基础控件
- WPF获取子、父窗体控件
- 【WPF开发】无人机HUD (Head Up Display)开源控件
- 比较不错的几款开源的WPF Charts报表控件
- C++文件选取(一)选择文件夹里的内容
- WPF实现带有Checkbox选择框的TreeView树控件