Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 在'上提供价值;System.Windows.Data.Binding';抛出异常_Wpf_Exception_Data Binding_Wpf 4.0 - Fatal编程技术网

Wpf 在'上提供价值;System.Windows.Data.Binding';抛出异常

Wpf 在'上提供价值;System.Windows.Data.Binding';抛出异常,wpf,exception,data-binding,wpf-4.0,Wpf,Exception,Data Binding,Wpf 4.0,我正在向文本框添加验证规则(以下是Adam Nathan的示例) 当启动一个应用程序时,我会遇到以下神秘的错误消息 在“System.Windows.Data.Binding”上提供值时引发异常 错误是什么意思?需要采取什么措施来解决问题? 这是完整的源代码 XAML 我认为这与TextBox.Text的默认绑定模式有关,它是双向的,并且对于该路径是必需的。您可以尝试将Mode=“OneWay”设置为绑定标记中的一个属性,该属性应该在这里工作,但我不能完全确定您的总体需求 <Windo

我正在向文本框添加验证规则(以下是Adam Nathan的示例)

当启动一个应用程序时,我会遇到以下神秘的错误消息

在“System.Windows.Data.Binding”上提供值时引发异常

错误是什么意思?需要采取什么措施来解决问题?

这是完整的源代码

XAML


我认为这与TextBox.Text的默认绑定模式有关,它是双向的,并且对于该路径是必需的。您可以尝试将
Mode=“OneWay”
设置为绑定标记中的一个属性,该属性应该在这里工作,但我不能完全确定您的总体需求

<Window.Resources>
    <Style x:Key="controlStyle" TargetType="{x:Type Control}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="RenderTransform">
                    <Setter.Value>
                        <RotateTransform Angle="20" />
                    </Setter.Value>
                </Setter>
                <Setter Property="Foreground" Value="Black" />
            </Trigger>
        </Style.Triggers>
        <Setter Property="FontSize" Value="22" />
        <Setter Property="Background" Value="Purple" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Height" Value="50" />
        <Setter Property="Width" Value="50" />
        <Setter Property="RenderTransformOrigin" Value=".5,.5" />
        <Setter Property="RenderTransform">
            <Setter.Value>
                <RotateTransform Angle="10" />
            </Setter.Value>
        </Setter>
        <Setter Property="TextBox.TextAlignment" Value="Right" />
    </Style>
    <Style x:Key="textBoxStyle" TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="Background" Value="Red" />
                <Setter Property="ToolTip" 
                        Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<StackPanel Orientation="Vertical">
    <StackPanel Orientation="Horizontal">
        <TextBox Style="{StaticResource textBoxStyle}">
            <TextBox.Text>
                <Binding>
                    <Binding.ValidationRules>
                        <local:JpgValidationRule />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
    </StackPanel>

    <StackPanel Orientation="Horizontal">
        <Button Style="{StaticResource controlStyle}">1</Button>
        <ComboBox Style="{StaticResource controlStyle}">
            <ComboBox.Items>2</ComboBox.Items>
        </ComboBox>
        <Expander Style="{StaticResource controlStyle}" Content="3" />
        <TabControl Style="{StaticResource controlStyle}">
            <TabControl.Items>4</TabControl.Items>
        </TabControl>
        <ToolBar Style="{StaticResource controlStyle}">
            <ToolBar.Items>5</ToolBar.Items>
        </ToolBar>
        <!--<InkCanvas Style="{StaticResource controlStyle}" />-->
        <TextBox Style="{StaticResource controlStyle}" Text="7" />
    </StackPanel>
</StackPanel>
using System.Globalization;
using System.IO;
using System.Windows.Controls;

namespace StylesDemo
{
    public class JpgValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            string fileName = value.ToString();

            // Reject nonexistent files:
            if (!File.Exists(fileName))
                return new ValidationResult(false, "Value is not a valid file!");

            // Reject files that don't end in .jpg:
            if (Path.GetExtension(fileName).ToUpper() != ".JPG")
                return new ValidationResult(false, "Value is not a .jpg file!");

            return new ValidationResult(true, null);
        }
    }
}