0

I have a dialog that use, that i simple can't understand why it will not bind.

I use Joe White's DialogCloser, from this answer

public static  class DialogCloser
{
    public static readonly DependencyProperty DialogResultProperty =
        DependencyProperty.RegisterAttached(
            "DialogResult",
            typeof(bool?),
            typeof(DialogCloser),
            new PropertyMetadata(DialogResultChanged));

    private static void DialogResultChanged(
        DependencyObject d,
        DependencyPropertyChangedEventArgs e)
    {
        var window = d as Window;
        if (window != null)
            window.DialogResult = e.NewValue as bool?;

    }
    public static void SetDialogResult(Window target, bool? value)
    {
        target.SetValue(DialogResultProperty, value);
    }
}

I use it only in DialogWindow.xml

<Window x:Class="ATS.Views.Common.DialogWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    Title="WindowDialog" 
    WindowStyle="SingleBorderWindow" 
    WindowStartupLocation="CenterOwner" SizeToContent="WidthAndHeight"
    xmlns:xc="clr-namespace:ATS.Views.Common"
    xc:DialogCloser.DialogResult="{Binding DialogResult}"
    ResizeMode="NoResize">
<ContentPresenter x:Name="DialogPresenter" Content="{Binding .}"></ContentPresenter>

The Binding it self is on a abstract class that I want to use for dialog windows.

public class DialogViewModel : BaseViewModel
{
    private bool? dialogResult;
    public bool? DialogResult
    {
        get
        {
            return dialogResult;
        }
        set
        {
            dialogResult = value;
            NotifyPropertyChange("DialogResult");
        }
    }
}

I open dialogs from ViewModels with this:

public bool? ShowDialog(string title, DialogViewModel dataContext)
    {
        var win = new DialogWindow();
        win.Title = title;
        win.DataContext = dataContext;

        return win.ShowDialog();    
    }

Now what seems to happen, even tho i create new dialog windows, is that the binding it self only get added once, and therefor it will only work on the first dialog that is created.

Is it something to do with binding to an abstact class that makes the bindings not work, due to same namespace and names?

Edit: Ended up using Mark's way of handling dialog boxes, which can be found here

Community
  • 1
  • 1
  • 1
    You'll have to provide more information. You say your dialog class is abstract? Partially declared classes can't be declared abstract, did you instead mean to say it implements a specific interface? This was one of the solutions I looked at when I wrote [my article about MVVM dialog boxes](http://www.codeproject.com/Articles/820324/Implementing-Dialog-Boxes-in-MVVM) (shameless plug alert). Moving the dialog result to the view model is a good idea but it's still not really MVVM because among other things it invokes the creation of the dialog box view directly from the view model. – Mark Feldman Aug 25 '15 at 00:57
  • I use an abstract viewmodel class for dialog viewmodels, its just so it has the databinding for DialogResult witch is used for the dialog closer, so when it has a value it willl tell the window that is should close and return the result. But it seems like the binding only attach to to first viewmodel that i bind to it, the second does not work. I can however keep using the first viewmodel for opening the dialog. So it seems like DialogWindow does only do the databing for dialog closer once – Henrik Bøgelund Lavstsen Aug 26 '15 at 09:09

0 Answers0