In a recent query from one of the users of C1FilePicker for WPF, I provided an implementation where customer was looking to display the complete path of the selected file in C1FilePicker. By default when OpenFileDialog box opens it shows only the name of the selected file. Through this blog I look to share this utility implementation. Even though this is relatively simple; however, it may help the users who are beginners and looking for similar type of customization with WPF controls. This blog provides customization of the C1FilePicker class which can be implemented for other WPF controls in a similar manner. In this implementation, we inherit the C1FilePicker class and handle the 'Browse' button. On the Mouse Down event for Browse button, suppress the default OpenFileDialog box and show a new dialog box with customized string. Following code block shows the implementation. C# Code
public class MyC1FilePicker : C1.WPF.C1FilePicker
{
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var btn = GetTemplateChild("Browse") as Button;
btn.PreviewMouseDown += new MouseButtonEventHandler(btn_PreviewMouseDown);
}
void btn_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
OpenFileDialog opd = new OpenFileDialog();
opd.Filter = this.Filter;
opd.FileName = this.SelectedFile.ToString();
opd.InitialDirectory = this.SelectedFile.DirectoryName;
if (opd.ShowDialog() == true)
this.SelectedFile = new System.IO.FileInfo(opd.FileName);
}
}
XML Code
xmlns:local="clr-namespace:WPF\_FilePicker\_CustomOpenFile"
<local:MyC1FilePicker x:Name="c1FilePicker1" Width="125" Height="25" Filter="Image Files(*.PNG;*.JPG;*.GIF)|*.PNG;*.JPG;*.GIF|All files (*.*)|*.*"/>
Refer to the attached sample for complete implementation. Download C# Sample