简体   繁体   English

WPF在绑定源中激发绑定更新的PropertyChanged事件

[英]wpf fire PropertyChanged event in binding source that binding gets updated

I'm trying to make some CustomControl Textboxes with validation features. 我正在尝试制作一些具有验证功能的CustomControl文本框。 (for example a number only textbox or a zip-code textbox etc.) It should realized in a .dll library file. (例如,仅数字文本框或邮政编码文本框等。)应在.dll库文件中实现。

My project contains a CustomControl for the textbox, a class wich handles the validations, and a ErrMsgGui CustomControl that should show a error message in a TextBlock (exmp.: Only numbers allowed...) 我的项目包含一个用于文本框的CustomControl,一个用于处理验证的类以及一个ErrMsgGui CustomControl,该控件应在TextBlock中显示错误消息(示例:仅允许数字...)

My problem is that I don't get the TextBlock Text updated when a method in the validation class is called 我的问题是,当调用验证类中的方法时,我没有更新TextBlock Text

Is there a way to trigger the PropertyChangeEvent which updates the Textblock text within the validaiton class? 有没有一种方法可以触发PropertyChangeEvent来更新validaiton类中的Textblock文本?

(Im quite new to wpf) (对wpf来说还很陌生)

Generic.xaml: Generic.xaml:

<Style TargetType="{x:Type local:NumTb}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:NumTb}">
                    <TextBox Background="{TemplateBinding Background}" Text="{Binding Source={StaticResource NumTbVm}, Path=NumTbText, UpdateSourceTrigger=PropertyChanged}"/> 
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>



<Style TargetType="{x:Type local:ErrMsgGui}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:ErrMsgGui}">
                <TextBlock Text="{ Binding  Source={StaticResource val}, Path=ErrMsgGuiText, UpdateSourceTrigger=PropertyChanged}" Background="{TemplateBinding Background}"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Validations.cs: Validations.cs:

    private const string ONLY_NUMBERS_REGEX = @"^[0-9]+$";  //Nur Zahlen

    private string _ErrMsgGuiText;
    public string ErrMsgGuiText
    {
        get { return _ErrMsgGuiText; }
        set
        {
            _ErrMsgGuiText = value;
            Debug.Print("QueryText: " + value);
            OnPropertyChanged("ErrMsgGuiText"); 
        }
    }


    public object[] onlyNumbers(string s2c, bool output)
    {
        object[] objRes = new object[2];
        bool result = true;
        string errMsg = ""; 

        Regex regex = new Regex(ONLY_NUMBERS_REGEX);

        if (s2c != null && s2c != "" && !regex.IsMatch(s2c)) 
        {
            result = false;
            errMsg = "Nur Zahlen sind zulässig";
        }

        objRes[0] = result;
        objRes[1] = errMsg;

        if (output == true) 
        { 
           ErrMsgGuiText = errMsg;

        }  
         return objRes;
    }

    public void onlyNumbers(string s2c)
    {
        onlyNumbers(s2c, true);
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    protected void OnPropertyChanged(string prop)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(prop));

        }

    }

NumTbViewModel.cs: NumTbViewModel.cs:

    Validations val = null;

    public NumTbViewModel() 
    {
        val = new Validations(); 
    }

    private string _NumTbText;
    public string NumTbText 
    {
        get { return _NumTbText; }
        set 
        {
            _NumTbText = value;
            this.OnPropertyChanged("NumTbText");
            val.onlyNumbers(_NumTbText);

        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    protected void OnPropertyChanged(string prop)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(prop));

        }

    }

It looks like the TextBlock source is looking at a static resource for the Validations class and the Validations called in your NumTbViewModel is NOT the same as the static resource. 看来TextBlock源正在为Validations类寻找静态资源,并且您的NumTbViewModel中调用的Validations与静态资源不同。 A solution could be to add a property to NumTbViewModel.cs and point your binding to that property so the Validations class instances will be the same. 一种解决方案是向NumTbViewModel.cs添加一个属性,然后将您的绑定指向该属性,以便Validations类实例相同。 In NumTbViewModel.cs add: 在NumTbViewModel.cs中添加:

Validations _val;
    public Validations Val
    {
        get { return _val; }
        set 
        {
            _val = value;
            this.OnPropertyChanged("Val");
        }
    }

Change your source and path in xaml binding on the TextBlock: 在TextBlock上的xaml绑定中更改源和路径:

<TextBlock Text="{ Binding  Source={StaticResource NumTbVm}, Path=Val.ErrMsgGuiText, UpdateSourceTrigger=PropertyChanged}" Background="{TemplateBinding Background}"/>

Another way: You could also set the Val property of your NumTbViewModel when you define your static resource like so: 另一种方法:在定义静态资源时,还可以设置NumTbViewModel的Val属性,如下所示:

<local:Validations x:Key="val" />
<local:NumTbViewModel x:Key="NumTbVm" Val="{StaticResource val}" />

Doing this you can keep the bindings like you originally had. 这样做可以使绑定保持原来的样子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM