繁体   English   中英

WPF UserControl 绑定问题

[英]WPF UserControl Binding Problem

我喜欢用自己的 Header 属性创建一个 UserControl。

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;
}

在 UserContol xaml 中:

Label Name="lbHeader" Grid.Column="0" Content="{Binding Path=Header}"

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

在 Windows xaml 中:

uc:SomeClass x:Name="AA2P" 

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


我需要使用 DataContext 来做其他事情。 我尝试使用依赖属性,但出了点问题。

public partial class tester : UserControl
{
    public tester()
    {
        InitializeComponent();
    }

    public string Header
    {
        get { return (string)GetValue(MyDependencyProperty); }
        set { SetValue(MyDependencyProperty, value); }
    }
    public static readonly DependencyProperty MyDependencyProperty =
    DependencyProperty.Register("MyDependencyProperty", typeof(string), typeof(string));

}


<UserControl ... x:Name="mainControl">
<TextBlock Text="{Binding ElementName=mainControl, Path=MyDependencyProperty}"/>
</UserControl>


<Window ...>
<my:tester Header="SomeText" />
</Window>

这没用。 我做错了什么? 谢谢!

最简单的方法是只使用 object 的 DataContext。 一种方法是直接在构造函数中,如下所示:

public SomeClass()
    {
        InitializeComponent();
        DataContext = this;
    }

设置 DataContext 将指定应该从哪里获取新数据。 在名为WPF 基本数据绑定常见问题解答的文章中有一些很棒的提示和信息。 阅读它以更好地了解 DataContex 的用途。 它是 WPF/C# 中必不可少的组件。


由于问题的更新而更新。

据我了解,您应该将DependencyProperty.Register的第一个参数更改为要绑定到的属性的名称,此处为"Header" ,以及您的 class 类型的第二个参数,此处为SomeClass 那会给你留下:

public static readonly DependencyProperty MyDependencyProperty =
    DependencyProperty.Register("Header", typeof(SomeClass), typeof(string));

但我很少使用依赖属性,所以我不肯定这是它,但它值得一试..

UserControl 本身需要稍后使用它的 DataContext。 但是 UserControl 内部的控件需要 UserControl 作为它们的 DataContext,否则它们也会从后面的使用上下文中继承 DataContext。 诀窍是将 UserControl 的子项的 DataContext 设置为 UserControl 的 DataContext,因此它现在可以使用 UserControl 的依赖属性。

<UserControl x:Class="MyControl.MyUserControl">
  <Grid DataContext="{Binding RelativeSource={RelativeSource FindAncestor,
      AncestorType=UserControl,AncestorLevel=1}}">...</Grid>
</UserControl>

如果您这样做,则 Grid 的子项可以具有简单的 {Binding dp's name} 而无需额外的 ElementName 参数。

如果您需要其他内容的数据上下文。 您还可以在 Binding 中使用 ElementName 属性。

    <UserControl
        x:Class="MyControl.MyUserControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="mainControl">
        <TextBlock Text="Binding ElementName=mainControl, Path=MyDependencyProperty}"/>
    </UserControl>

[编辑]

我应该添加一些东西。 使“Header”属性成为依赖属性,这将使您的生活更轻松。 在 UI 控件中,您应该使属性几乎总是一个依赖属性,您控件的每个设计者或用户都会感谢您。

暂无
暂无

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

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