Quantcast
Channel: wpftoolkit Discussions Rss Feed
Viewing all articles
Browse latest Browse all 2157

New Post: File Browser from Property Grid

$
0
0
Thank you for the response. I think I am getting close, however, I am unable to get the file dialog to bind back the Xceed Property item.
    public FrameworkElement ResolveEditor(PropertyItem propertyItem)
    {
        var file = String.IsNullOrEmpty(propertyItem.Value?.ToString()) ? "Select File..." : propertyItem.Value?.ToString();
        var editField = new Button();
        editField.Content = file;
        editField.Click += (s, a) => 
        {
            var newFile = GetFile(propertyItem.Value?.ToString());
            propertyItem.Value = newFile;
            editField.Content = newFile;
        };

        var binding = new Binding("Value");
        binding.Source = propertyItem;
        binding.ValidatesOnExceptions = true;
        binding.ValidatesOnDataErrors = true;
        binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;

        BindingOperations.SetBinding(editField, TextBox.TextProperty, binding);
        return editField;
    }

    private String GetFile(String initial)
    {
        var dlg = new OpenFileDialog();
        dlg.CheckFileExists = true;
        dlg.RestoreDirectory = true;
        dlg.Multiselect = false;
        dlg.DefaultExt = "*.SAI";
        dlg.Filter = "Sage Files|*.SAI";
        dlg.InitialDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Simply Accounting/DATA");
        if(!Directory.Exists(dlg.InitialDirectory))
        {
            dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        }
        var result = dlg.ShowDialog();
        if(!result.HasValue)
        {
            return "";
        }
        if(result.Value)
        {
            return dlg.FileName;
        }
        return initial;
    }
}
After the user selects a file, the button id updated to display the filename, but the property does not get set on the settings object. What am I missing?

Viewing all articles
Browse latest Browse all 2157

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>