WPF:尝试绑定到静态属性时出现System.Windows.Markup.XamlParseException

时间:2018-09-21 05:45:50

标签: c# wpf

这是什么问题?我和xaml代码有关。 我正在尝试从其他类更新静态属性并更新文本框。 疯狂的是,此示例可以正常工作: https://www.c-sharpcorner.com/UploadFile/mahesh/binding-static-properties-in-wpf-4-5/

谢谢。

我收到此消息:

System.Windows.Markup.XamlParseException
InnerException  {"Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type."}   System.Exception {System.ArgumentException}

我的xaml:

<TextBox x:Name="txtbx" Text="{Binding Path=(local:Class3.Count), 
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  Grid.Row="1" Width="40" 
Height="20" ></TextBox>

我的代码:

class Class3
{
    private static string count;
    public static string Count
    {
        get
        { return count; }
        set
        {
            if (value != count)
            {
                count = value;
                OnStaticPropertyChanged(); 
            }
        }
    }

    public static event EventHandler StaticPropertyChanged;
    static void OnStaticPropertyChanged() 
    {
        var handler = StaticPropertyChanged;
        if (handler != null)
        {
            handler(null, EventArgs.Empty);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

EventHandlerStaticPropertyChanged事件的错误委托类型。

类似于INotifyPropertyChanged接口的PropertyChanged事件,它必须为PropertyChangedEventHandler

class Class3
{
    private static string count;
    public static string Count
    {
        get { return count; }
        set
        {
            if (value != count)
            {
                count = value;
                OnStaticPropertyChanged(nameof(Count));
            }
        }
    }

    public static event PropertyChangedEventHandler StaticPropertyChanged;

    private static void OnStaticPropertyChanged(string propertyName)
    {
        StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
    }
}