代码之家  ›  专栏  ›  技术社区  ›  Matt B

在代码中设置“依赖属性”

  •  0
  • Matt B  · 技术社区  · 16 年前

    我今天很忙…

    我在一个名为actionscreen的类中使用了以下代码来延迟依赖项属性:

    #region Dependency Properties & Methods
    
    public string DescriptionHeader
    {
        get { return (string)GetValue(DescriptionHeaderProperty); }
        set { SetValue(DescriptionHeaderProperty, value); }
    }
    
    // Using a DependencyProperty as the backing store for DescriptionHeader.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DescriptionHeaderProperty =
        DependencyProperty.Register("DescriptionHeader", typeof(string), typeof(ActionScreen), new UIPropertyMetadata("xxx"));
    
    #endregion
    

    我在XAML中绑定到该属性,因此:

                        <GridViewColumn DisplayMemberBinding="{Binding Description}" Header="{Binding DescriptionHeader}" Width="350" />
    

    现在,当我接收到一个事件时,我希望能够从我的代码后面设置参数-但它不起作用:

    public string DescColText { set { this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { DescriptionHeader = value; })); } }
    
    2 回复  |  直到 16 年前
        1
  •  1
  •   Queso    16 年前

    我也遇到过同样的问题。Depedency属性更改似乎不会触发属性实际上已更改的通知。你必须手动操作。在代码隐藏中,实现inotifyPropertyChanged接口,并在依赖项属性注册中添加propertyChangedCallback委托。(示例)

    public static readonly DependencyProperty DescriptionHeaderProperty = DependencyProperty.Register("DescriptionHeader", typeof(string), typeof(ActionScreen), new UIPropertyMetadata("xxx", new PropertyChangedCallback(DisplayTextChange)));
    
        private static void DisplayTextChange(DependencyObject dpo, DependencyPropertyChangedEventArgs args)
                {
                    ((WhatEverYourClassNameIs)dpo).NotifyPropertyChanged("DescriptionHeader");
    
                }
    

    以下是inotifyPropertyChanged实现的示例

    public event PropertyChangedEventHandler PropertyChanged;
    
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
    

    这将允许您的用户界面看到发生了更改。

        2
  •  1
  •   Matt B    16 年前

    我没有设置页面的DataContext。

    显然我需要这么做

    this.DataContext = this;