Skip to content

Instantly share code, notes, and snippets.

@JohanLarsson
Created July 11, 2013 17:45
Show Gist options
  • Save JohanLarsson/5977596 to your computer and use it in GitHub Desktop.
Save JohanLarsson/5977596 to your computer and use it in GitHub Desktop.
<UserControl x:Class="Namespace.FileControl"
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">
<Grid Height="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="25"/>
</Grid.ColumnDefinitions>
<Label x:Name="Label" Grid.Column="0"/>
<TextBox x:Name="PathTb" Grid.Column="1" TextChanged="PathTb_OnTextChanged"/>
<Button Grid.Column="2" Content="..." ToolTip="Browse for file" Click="BrowseClick"/>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 Microsoft.Win32;
namespace Namespace
{
/// <summary>
/// Interaction logic for FileControl.xaml
/// </summary>
public partial class FileControl : UserControl
{
public FileControl()
{
InitializeComponent();
}
public static readonly DependencyProperty FilterProperty =
DependencyProperty.Register("Filter", typeof(string), typeof(FileControl), new PropertyMetadata(default(string)));
public string Filter
{
get { return (string)GetValue(FilterProperty); }
set { SetValue(FilterProperty, value); }
}
public static readonly DependencyProperty LabelTextProperty =
DependencyProperty.Register("LabelText", typeof(string), typeof(FileControl), new PropertyMetadata(default(string), (o, e) => ((FileControl)o).Label.Content = e.NewValue));
public string LabelText
{
get { return (string)GetValue(LabelTextProperty); }
set { SetValue(LabelTextProperty, value); }
}
public static readonly DependencyProperty PathProperty =
DependencyProperty.Register("Path", typeof (string), typeof (FileControl), new PropertyMetadata(default(string), (o, e) => ((FileControl)o).PathTb.Text = (string) e.NewValue));
public string Path
{
get { return (string) GetValue(PathProperty); }
set
{
SetValue(PathProperty, value);
}
}
private void BrowseClick(object sender, RoutedEventArgs e)
{
var fileDialog = new OpenFileDialog
{
Filter = Filter,
Multiselect = false
};
bool? showDialog = fileDialog.ShowDialog();
if (showDialog == true)
{
Path = fileDialog.FileName;
SetCurrentValue(PathProperty, fileDialog.FileName);
}
}
private void PathTb_OnTextChanged(object sender, TextChangedEventArgs e)
{
Path = PathTb.Text;
//SetCurrentValue(PathProperty, PathTb.Text);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment