简体   繁体   中英

My FolderBrowserDialog in WPF isn't displayed

I use WPF and want to show a FolderBrowserDialog therefore I have created a WPF-UserControl shell:

<UserControl x:Class="FolderBrowserDialog"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</UserControl>

and it's code-behind:

    public static readonly DependencyProperty ShowDialogProperty =
        DependencyProperty.Register("ShowDialog", typeof (bool),
            typeof (SaveFileDialog));

    public bool ShowDialog
    {
        get { return (bool) GetValue(ShowDialogProperty); }
        set
        {
            SetValue(ShowDialogProperty, value);
            if (value)
            {
                var folderBrowserDialog = new FolderBrowserDialog();
                if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                {
                    PathToFolder = folderBrowserDialog.SelectedPath;
                }
            }
        }
    }

I use my FolderBrowser-UserControl on another WPF UserControl where I need this dialog:

<UserControl:FolderBrowserDialogShowDialog="{Binding ShowFolderBrowser, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

On my ViewModel is a public property ShowFolderBrowser with the onPropertyChanged event but the dialog isn't displayed.

I also have not any binding errors in the output panel...

When you use a Property Dependency you shouldn't put code in get and set. The SetValue is called automatically if you set the value from Xaml.

Instead you can use a callback for notification, something like

public static readonly DependencyProperty ShowDialogProperty =
        DependencyProperty.Register("ShowDialog", typeof (bool),
            typeof (SaveFileDialog), new UIPropertyMetadata(false, ShowDialogChangedCallback));

private static void ShowDialogChangedCallback(DependencyObject source, 
        DependencyPropertyChangedEventArgs e)
{
    YourControl control = source as YourControl;
    bool showdialog = (bool)e.NewValue;
    // logic here
}

Further check your dependency property declaration because typeof (SaveFileDialog) can ben wrong, but maybe you have mispelled some name in this example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM