Not controlled exception in WPF XAML: The specifie

2019-08-19 01:23发布

I have a MVVM WPF application. I have below converter:

public class PrintIconVisibilityValueConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values[0] == null || values[1] == null) return Visibility.Collapsed;

        int item1 = (int)values[0];
        string item2 = (string)values[1];

        if (item1 > 0 || !string.IsNullOrEmpty(item2))
        {
            return Visibility.Visible;
        }
        else
        {
            return Visibility.Collapsed;
        }
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

From my view I do:

<Window.Resources>
   <classes:PrintIconVisibilityValueConverter x:Key="PrintIconVisibilityValueConverter"/>
</Window.Resources>

then I have an image in this view:

<Image Source="/MyImages;component/Images/PrintIco.png" 
       Height="15" Margin="20 0 5 0">
    <Image.Visibility>
            <MultiBinding Converter="{StaticResource PrintIconVisibilityValueConverter}">
                <Binding Path="Item1" />
                <Binding Path="Item2" />
            </MultiBinding>
    </Image.Visibility>
</Image>

Item1 and Item2 are public properties in view model:

private string _item2 = string.Empty;
public string Item2
{
    get
    {
        return _item2;
    }

    set
    {
        if (_item2 == value) return;

        _item2 = value;
        OnPropertyChanged("Item2");
    }
}

private int _item1;
public int Item1
{
    get
    {
        return _item1;
    }

    set
    {
        if (_item1 == value) return;

        _item1 = value;
        OnPropertyChanged("Item1");
    }
}

It compiles correctly and I can execute the application without problems but in design time, the view is not show, an error says Not controlled exception and points to the line:

int item1 = (int)values[0];

within PrintIconVisibilityValueConverter class.

Below the screenshots of the exception shown on view:

enter image description here enter image description here enter image description here

标签: c# wpf xaml mvvm
1条回答
做自己的国王
2楼-- · 2019-08-19 01:39

Some suggestions;

Call the GetIsInDesignMode method in your converter and return immediately if it returns true:

public class PrintIconVisibilityValueConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            return Visibility.Visible;
        ...
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Set the DataContext in XAML:

<Window.DataContext>
    <local:ViewModel />
</Window.DataContext>

Set the design time data context:

<Window ... d:DataContext ="{d:DesignInstance {x:Type local:ViewModel}, IsDesignTimeCreatable=True}">

Or Disable XAML UI designer

查看更多
登录 后发表回答