为System.Windows.StaticResourceExtension'提供价值。抛出一个例外

时间:2016-02-13 09:21:06

标签: wpf xaml mvvm mvvm-light mvvm-toolkit

我试图通过Converter绑定WPF中的Window可见性。我收到了错误。 System.Windows.StaticResourceExtension System.Windows.StaticResourceExtension

我在下面提供我的代码。 我的观点是 enter image description here

<Window x:Class="UI.ChildWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:UI"
    xmlns:UtilityValue="clr-namespace:UI.Utility"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"        
    mc:Ignorable="d"
    Title="ChildWindow" Height="70" Width="400" WindowStartupLocation="CenterScreen" WindowStyle="None" 
    Visibility="{Binding WindowVisibility, Converter={StaticResource VisibilityConverter}, Mode=TwoWay}">    
<Window.Resources>
    <UtilityValue:TextInputToVisibilityConverter x:Key="TextInputToVisibilityConverter"></UtilityValue:TextInputToVisibilityConverter>
    <UtilityValue:EventToCommandBehavior x:Key="CommandBehavior"></UtilityValue:EventToCommandBehavior>
    <SolidColorBrush x:Key="brushWatermarkBackground" Color="White" />
    <SolidColorBrush x:Key="brushWatermarkForeground" Color="LightSteelBlue" />
    <SolidColorBrush x:Key="brushWatermarkBorder" Color="Indigo" />
    <UtilityValue:BooleanToVisibilityConverter x:Key="VisibilityConverter"></UtilityValue:BooleanToVisibilityConverter>      
    <Style x:Key="EntryFieldStyle" TargetType="Grid" >
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="Margin" Value="2" />
    </Style>
</Window.Resources>

My Viewmodel如下:

区域WindowVisibility

    private bool _windowVisibility=true;
    public bool WindowVisibility
    {
        get { return _windowVisibility; }
        set { _windowVisibility = value;
            OnPropertyChanged("WindowVisibility");
        }
    }

    #endregion

转换器

public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                if ((bool)value)
                    return Visibility.Visible;
                else
                    return Visibility.Collapsed;
            }
            else
                return Visibility.Collapsed;
        }

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

此转换器可与其他控件正常工作,但使用此窗口时,它无法正常工作。 我想知道转换器是使用Window级还是仅适用于控件?

1 个答案:

答案 0 :(得分:2)

Resources集合中转换器的声明应该在引用转换器之前。

你可以这样解决:

<Window x:Class="UI.ChildWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:UI"
        xmlns:UtilityValue="clr-namespace:UI.Utility"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"        
        mc:Ignorable="d"
        Title="ChildWindow" Height="70" Width="400" WindowStartupLocation="CenterScreen" WindowStyle="None">
    <Window.Resources>
        <UtilityValue:TextInputToVisibilityConverter x:Key="TextInputToVisibilityConverter"></UtilityValue:TextInputToVisibilityConverter>
        <UtilityValue:EventToCommandBehavior x:Key="CommandBehavior"></UtilityValue:EventToCommandBehavior>
        <SolidColorBrush x:Key="brushWatermarkBackground" Color="White" />
        <SolidColorBrush x:Key="brushWatermarkForeground" Color="LightSteelBlue" />
        <SolidColorBrush x:Key="brushWatermarkBorder" Color="Indigo" />
        <UtilityValue:BooleanToVisibilityConverter x:Key="VisibilityConverter"></UtilityValue:BooleanToVisibilityConverter>
        <Style x:Key="EntryFieldStyle" TargetType="Grid" >
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="Margin" Value="2" />
        </Style>
    </Window.Resources>

    <Window.Visibility>
        <Binding Path="WindowVisibility" Converter="{StaticResource VisibilityConverter}" Mode="TwoWay" />
    </Window.Visibility>
</Window>