Created
March 31, 2014 11:35
-
-
Save odugen/9890434 to your computer and use it in GitHub Desktop.
WPF FolderDialog Behavior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class FolderDialogBehavior : Behavior<Button> | |
| { | |
| public string SetterName { get; set; } | |
| protected override void OnAttached() | |
| { | |
| base.OnAttached(); | |
| AssociatedObject.Click += OnClick; | |
| } | |
| protected override void OnDetaching() | |
| { | |
| AssociatedObject.Click -= OnClick; | |
| } | |
| private void OnClick(object sender, RoutedEventArgs e) | |
| { | |
| var dialog = new FolderBrowserDialog(); | |
| var result = dialog.ShowDialog(); | |
| if (result == DialogResult.OK && AssociatedObject.DataContext != null) | |
| { | |
| var propertyInfo = AssociatedObject.DataContext.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public) | |
| .Where(p => p.CanRead && p.CanWrite) | |
| .Where(p => p.Name.Equals(SetterName)) | |
| .First(); | |
| propertyInfo.SetValue(AssociatedObject.DataContext, dialog.SelectedPath, null); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Button Grid.Column="3" Content="..."> | |
| <Interactivity:Interaction.Behaviors> | |
| <Behavior:FolderDialogBehavior SetterName="SomeFolderPathPropertyName"/> | |
| </Interactivity:Interaction.Behaviors> | |
| </Button> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment