AttachmentEditor: Filter the list of attachments

723
5
Jump to solution
06-25-2014 11:37 AM
GreggCornetski
New Contributor III
Is it possible to filter the files that are displayed in the AttachmentEditor's ListBox sub-component? By default, the AttachmentEditor's internal ListBox is bound to all of the files (AttachmentInfos) associated with the current graphic. I would like to be able to tell the AttachmentEditor to only list files of, say, type .pdf, or limit the files displayed using some flexible query of the AttachmentInfo properties.

This doesn't seem possible because no kind of filter/query of the attachments is built into the AttachmentEditor, nor does the attachment editor expose its ListBox sub-component or its internal list of attachments.

Perhaps the easiest solution is to grab the AttachmentEditor source code and tweak as necessary.

Note well, I'm not at all referring to the Filter, FilterIndex, and Multiselect properties which control the open file dialog.
0 Kudos
1 Solution

Accepted Solutions
AhmedEl-Sisi
Occasional Contributor III
A good idea is to make a new control inherits from Attachment Editor control then override onApplytemplate method to get the ItemsControl object.
After that you can listen on ItemsSource property changed via DependencyProperty.RegisterAttached Method.
Check this post for more information.

     
  ItemsControl attachmentListControl = null;         public override void OnApplyTemplate()         {             base.OnApplyTemplate();             attachmentListControl = GetTemplateChild("AttachmentList") as ItemsControl;             RegisterForNotification("ItemsSource", this.attachmentListControl, new PropertyChangedCallback(ItemsSourceChanged));         }          public void ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)         {             System.Collections.ObjectModel.ObservableCollection<AttachmentInfo> attachmentInfoList = e.NewValue as System.Collections.ObjectModel.ObservableCollection<AttachmentInfo>;             attachmentListControl.ItemsSource = attachmentInfoList.Where(x => x.Name.Contains(".pdf")); //or better check content type          }         public void RegisterForNotification(string propertyName, FrameworkElement element, PropertyChangedCallback callback)         {             Binding b = new Binding(propertyName) { Source = element };             var prop = System.Windows.DependencyProperty.RegisterAttached("ListenAttached" + propertyName, typeof(object), typeof(UserControl), new System.Windows.PropertyMetadata(callback));             element.SetBinding(prop, b);         }


you can also add another property for your filter to be more dynamic.

Hope this helps.
Regards,

View solution in original post

0 Kudos
5 Replies
AhmedEl-Sisi
Occasional Contributor III
My first thought is to handle the visibility of the item according to its file type.
you can bind the visibility of the Item Template to the uri with converter to set visibility to Visible if it matched your filter.
It's a quick solution and if you have more complex business you should implement your own control.

Check this post for something similar.

Regards,
0 Kudos
AhmedEl-Sisi
Occasional Contributor III
A good idea is to make a new control inherits from Attachment Editor control then override onApplytemplate method to get the ItemsControl object.
After that you can listen on ItemsSource property changed via DependencyProperty.RegisterAttached Method.
Check this post for more information.

     
  ItemsControl attachmentListControl = null;         public override void OnApplyTemplate()         {             base.OnApplyTemplate();             attachmentListControl = GetTemplateChild("AttachmentList") as ItemsControl;             RegisterForNotification("ItemsSource", this.attachmentListControl, new PropertyChangedCallback(ItemsSourceChanged));         }          public void ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)         {             System.Collections.ObjectModel.ObservableCollection<AttachmentInfo> attachmentInfoList = e.NewValue as System.Collections.ObjectModel.ObservableCollection<AttachmentInfo>;             attachmentListControl.ItemsSource = attachmentInfoList.Where(x => x.Name.Contains(".pdf")); //or better check content type          }         public void RegisterForNotification(string propertyName, FrameworkElement element, PropertyChangedCallback callback)         {             Binding b = new Binding(propertyName) { Source = element };             var prop = System.Windows.DependencyProperty.RegisterAttached("ListenAttached" + propertyName, typeof(object), typeof(UserControl), new System.Windows.PropertyMetadata(callback));             element.SetBinding(prop, b);         }


you can also add another property for your filter to be more dynamic.

Hope this helps.
Regards,
0 Kudos
GreggCornetski
New Contributor III
Brilliant. Thank you. I implemented a converter and it works fine.

Something like this:

    public class FileVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string fileName = System.Convert.ToString(value); // Current file name
            string extension = System.Convert.ToString(parameter).ToLower(); // Visible extension, e.g. ".pdf"

            if (System.IO.Path.GetExtension(fileName).ToLower() == extension)
            {
                return Visibility.Visible;
            }
            else
            {
                return Visibility.Collapsed;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


        
<DataTemplate x:Key="AttachmentEditorTemplate">
            <Grid Margin="3" Visibility="{Binding Name, Converter={StaticResource FileVisibilityConverter}, ConverterParameter='.pdf'}">
...
0 Kudos
GreggCornetski
New Contributor III
A good idea is to make a new control inherits from Attachment Editor control then override onApplytemplate method to get the ItemsControl object.
After that you can listen on ItemsSource property changed via DependencyProperty.RegisterAttached Method.
Check this post for more information.

     
  ItemsControl attachmentListControl = null;
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            attachmentListControl = GetTemplateChild("AttachmentList") as ItemsControl;
            RegisterForNotification("ItemsSource", this.attachmentListControl, new PropertyChangedCallback(ItemsSourceChanged));
        }

        public void ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            System.Collections.ObjectModel.ObservableCollection<AttachmentInfo> attachmentInfoList = e.NewValue as System.Collections.ObjectModel.ObservableCollection<AttachmentInfo>;
            attachmentListControl.ItemsSource = attachmentInfoList.Where(x => x.Name.Contains(".pdf")); //or better check content type

        }
        public void RegisterForNotification(string propertyName, FrameworkElement element, PropertyChangedCallback callback)
        {
            Binding b = new Binding(propertyName) { Source = element };
            var prop = System.Windows.DependencyProperty.RegisterAttached("ListenAttached" + propertyName, typeof(object), typeof(UserControl), new System.Windows.PropertyMetadata(callback));
            element.SetBinding(prop, b);
        }


you can also add another property for your filter to be more dynamic.

Hope this helps.
Regards,


From what I can understand this approach would work, too. How do you know the ESRI AttachmentEditor contains an ItemsControl named "AttachmentList" (and that it is an ItemsControl)?
0 Kudos
AhmedEl-Sisi
Occasional Contributor III
From what I can understand this approach would work, too. How do you know the ESRI AttachmentEditor contains an ItemsControl named "AttachmentList" (and that it is an ItemsControl)?

I tested this approach and it works fine and it gives you more control on your control.
It filters the attachments, the first solution is just hiding the item which is not recommended.
you can find Attachment Editor Control source on CodePlex with all other toolkit controls.
0 Kudos