相关疑难解决方法(0)

WPF DependencyProperties

我刚刚意识到我一直在强制绑定/依赖属性,而不是从根本上理解这个概念.

继承人属性:

public string Problem
{
    get { return (string)GetValue(ProblemProperty); }
    set { SetValue(ProblemProperty, value); }
}

public static readonly DependencyProperty ProblemProperty =
    DependencyProperty.Register(
    "Problem",
    typeof(string),
    typeof(TextBox));
Run Code Online (Sandbox Code Playgroud)

XAML如此:

<TextBlock Text="{Binding Path=Problem}"/>
Run Code Online (Sandbox Code Playgroud)

我手动将Problem属性设置为对象的构造函数中的值,但它不会相应地更新TextBlock...有任何想法吗?我已经尝试过Mode="OneWay"并且Mode="TwoWay"仍然无法正常工作.

我以为这应该自动运行?或者我从根本上弄错了什么?

谢谢

wpf dependency-properties

6
推荐指数
1
解决办法
8719
查看次数

WPF 用户控件绑定问题

我喜欢创建一个具有自己的标题属性的用户控件。

public partial class SomeClass: UserControl, INotifyPropertyChanged
{
    public SomeClass()
    {
        InitializeComponent();
    }

    private string header;
    public string Header
    {
        get { return header; }
        set 
        { 
            header = value;
            OnPropertyChanged("Header");
        }
    }

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

    public event PropertyChangedEventHandler PropertyChanged;
}
Run Code Online (Sandbox Code Playgroud)

在 UserContol xaml 中:

Label Name="lbHeader" Grid.Column="0" Content="{Binding Path=Header}"
Run Code Online (Sandbox Code Playgroud)

如果我设置该值:AA2P.Header = "SomeHeeaderText";label.Caption不会改变。我该如何解决这个问题?

在 Windows xaml 中:

uc:SomeClass x:Name="AA2P" 
Run Code Online (Sandbox Code Playgroud)

如果我直接给标签一个值(lbHeader.Content = header;)而不是OnPropertyChanged("Header");它的工作,但是,为什么它不能与 …

c# wpf

4
推荐指数
1
解决办法
8979
查看次数

标签 统计

wpf ×2

c# ×1

dependency-properties ×1