StaticResourceExtension

StaticResourceExtension

本文介绍了在“System.Windows.StaticResourceExtension"上提供值引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Converter 在 WPF 中绑定窗口的可见性.我收到错误.System.Windows.StaticResourceExtensionSystem.Windows.StaticResourceExtension

I am trying to bind Visibility of Window in WPF by Converter. I am getting the error.System.Windows.StaticResourceExtensionSystem.Windows.StaticResourceExtension

我在下面提供我的代码.我的观点是在此处输入图片描述

I am providing my code below.My View is 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>

我的视图模型如下:

    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();
        }
    }

此转换器与其他控件一起正常工作,但与此窗口一起无法正常工作.我想知道转换器是在窗口级别上工作还是仅在控件上工作?

This converter works properly with other controls but with this Window it is not working . I would like to know does converter works with Window level or only on controls?

推荐答案

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

The declaration of the converter in the Resources collection should be before the reference to the converter.

你可以这样修复它:

<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>

这篇关于在“System.Windows.StaticResourceExtension"上提供值引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 14:14