Hello All,
I just wanted to submit a solution I found that allows me to attach a file browser dialog to a textbox field of a data form (see attached example1.png). Basically I wanted a browse button next to the text box that would open a filedialog and replace the textbox's text property with the selected file's path. I found this cool class in a blog post ( blogpost ) that allow someone to attach a filebrowser dialog to a textbox and re-engineered it in VB.NET (see the blog posting for the C# version). I then created a User Control that contains the button and textbox and attaches to a custom filter field. Next I created a class call DataFormImageFileBrowser that Inherits from the DataFormDataField and replaces the textbox with the user control. Finally I bound the custom DataFormDataField in the AutoGeneratingField event and viola it works... here my code for the OpenFileDialogEx and the DataFormImageFileBrowser
Here is the code for the UserControl:
and its code behind:
here is the code for the AutoGeneratingField for the RadDataForm
I thought this was pretty cool so I thought I would share...
I just wanted to submit a solution I found that allows me to attach a file browser dialog to a textbox field of a data form (see attached example1.png). Basically I wanted a browse button next to the text box that would open a filedialog and replace the textbox's text property with the selected file's path. I found this cool class in a blog post ( blogpost ) that allow someone to attach a filebrowser dialog to a textbox and re-engineered it in VB.NET (see the blog posting for the C# version). I then created a User Control that contains the button and textbox and attaches to a custom filter field. Next I created a class call DataFormImageFileBrowser that Inherits from the DataFormDataField and replaces the textbox with the user control. Finally I bound the custom DataFormDataField in the AutoGeneratingField event and viola it works... here my code for the OpenFileDialogEx and the DataFormImageFileBrowser
Imports SystemImports Microsoft.Win32Imports Telerik.WindowsImports Telerik.Windows.ControlsPublic Class OpenFileDialogEx Public Shared ReadOnly FilterProperty As DependencyProperty = DependencyProperty.RegisterAttached("Filter", GetType(String), GetType(OpenFileDialogEx), New PropertyMetadata("All documents (.*)|*.*", Sub(d, e) AttachFileDialog(DirectCast(d, TextBox), e))) Public Shared Function GetFilter(element As UIElement) As String Return DirectCast(element.GetValue(FilterProperty), String) End Function Public Shared Sub SetFilter(element As UIElement, value As String) element.SetValue(FilterProperty, value) End Sub Private Shared Sub AttachFileDialog(textBox As TextBox, args As DependencyPropertyChangedEventArgs) Dim parent = DirectCast(textBox.Parent, Panel) ''added this because there was a problem with double binding and it was causing the filebrowser dialog to pop up twice Dim tag = TryCast(parent.Tag, String) If Not IsNothing(tag) Then Return parent.Tag = "already bound" AddHandler parent.Loaded, Sub() Dim button = DirectCast(parent.Children.Cast(Of Object)().FirstOrDefault(Function(x) TypeOf x Is Button), Button) Dim filter = DirectCast(args.NewValue, String) AddHandler button.Click, Sub(s, e) Dim dlg = New OpenFileDialog() dlg.Filter = filter Dim result = dlg.ShowDialog() If result = True Then textBox.Text = dlg.FileName End If End Sub End Sub End SubEnd ClassPublic Class DataFormImageFileBrowser Inherits DataFormDataField Protected Overrides Function GetControlBindingProperty() As DependencyProperty Return TextBox.TextProperty End Function Protected Overrides Function GetControl() As Control Dim fd = New OpenFileDialogUC("Image file (pdf,png,gif,jpg,bmp,tif,emf,wmf)|*.pdf;*.png;*.gif;*.jpg;*.bmp;*.jpeg;*.tiff;*.tif;*.emf;*.wmf") Dim dependencyProperty As DependencyProperty = GetControlBindingProperty() If Not IsNothing(DataMemberBinding) Then Dim binding = DataMemberBinding fd.FileNameTbx.SetBinding(dependencyProperty, binding) End If Return fd End FunctionEnd ClassHere is the code for the UserControl:
<UserControl x:Class="OpenFileDialogUC" xmlns:src="clr-namespace:CarWashInventoryManagement" mc:Ignorable="d" > <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <TextBox x:Name="FileNameTbx" MinWidth="100" src:OpenFileDialogEx.Filter="{Binding FilterStr}" Grid.Column="0" /> <Button Grid.Column="1">Browse</Button> </Grid></UserControl>and its code behind:
Public Class OpenFileDialogUC Public Property FilterStr As String = String.Empty Public Sub New(_filter As String) FilterStr = _filter DataContext = Me InitializeComponent() End SubEnd Classhere is the code for the AutoGeneratingField for the RadDataForm
ElseIf e.PropertyName = "FileName" Then Dim df = New DataFormImageFileBrowser() df.Label = e.DataField.Label df.DataMemberBinding = e.DataField.DataMemberBinding e.DataField = df e.DataField.Label = "File Name"I thought this was pretty cool so I thought I would share...