Quantcast
Viewing all articles
Browse latest Browse all 18858

MVVM DialogResult Validation

I implemented a way to use mvvm and dialog result using attached properties.

        public static bool? GetDialogResult(DependencyObject obj)
        {
            return (bool?)obj.GetValue(DialogResultProperty);
        }
        public static void SetDialogResult(DependencyObject obj, bool? value)
        {
            obj.SetValue(DialogResultProperty, value);
        }
        public static readonly DependencyProperty DialogResultProperty = DependencyProperty.RegisterAttached("DialogResult", typeof(bool?), typeof(ButtonHelper), new UIPropertyMetadata
        {
            PropertyChangedCallback = (obj, e) =>
            {
                Button button = obj as Button;
                if (button == null)
                {
                    throw new InvalidOperationException("ButtonHelper.DialogResult is only valid on Button controls");
                }
                button.Click += (sender, e2) =>
                {
                    Window.GetWindow(button).DialogResult = GetDialogResult(button);   
                };
            }
        });

So then in xaml you just do this:

<Button Content="Finish" ap:ButtonHelper.DialogResult="True" >
Now I want to do a bit of validation.  If conditions are right set dialog result = true and close the page.  But of some conditions aren't met I want to display a messagebox to the user informing them and giving them the option to proceed (set dialogresult = true) or cancel (close the messagebox and don't close the existing window).  I've tried using binding on the dialog result property but I need it to evaluate when the button is clicked.  I also tried adding another attached property of type delegate to run a validation function.  I can get the messagebox to display when running my validation delegate but i need it to wait for the result of my button click and the way i have it currently coded it doesn't wait and just returns (so i never really see my messagebox).  Any ideas how to acheive what I want?

Viewing all articles
Browse latest Browse all 18858

Trending Articles



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