如何使自定义的XAML用户控件可绑定?

6

我正在制作一个简单的示例来学习如何创建可绑定的用户控件。我已经创建了一个简单的类:

class Person
{
    public string firstName;
    public string lastName;    
    public Person(string first, string last)
    {
        firstName = first;
        lastName = last;
    }
}

以下是一个非常简单的用户控件:

<UserControl x:Class="Example.ExampleHRControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock x:Name="textFirstName"></TextBlock>
        <TextBlock x:Name="textLastName"></TextBlock>
    </Grid>
</UserControl>

我想知道的是,我需要做什么才能像使用普通控件一样在上下文中使用用户控件。我可以将其添加到 MainWindow 中:
<local:ExampleHRControl x:Name="Hr1"></local:ExampleHRControl>

然后我可以通过代码后台来访问它并添加值:

Hr1.textFirstName.Text = "John";
Hr1.textLasttName.Text = "Doe";

我希望能够创建一个Person类的实例,并将主窗口上的控件简单地绑定到Person类。

2个回答

6

为使此工作正常,您需要完成以下两个步骤。

在您的代码后台中,添加一个依赖属性,以使您的控件了解您想要的Person对象:

   public static readonly DependencyProperty PersonProperty =
                          DependencyProperty.Register("Person", typeof(Person),
                                                      typeof(ExampleHRControl));

   public Person Person
   {
      get { return (Person)GetValue(PersonProperty); }
      set { SetValue(PersonProperty, value); }
   }

在你的XAML中,将你的代码绑定到数据上下文,并将绑定添加到你的人物对象中:

<UserControl x:Class="Example.ExampleHRControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
         x:Name="This">
    <Grid>
        <TextBlock x:Name="{Binding Path=Person.FirstName, ElementName=This}"/>
        <TextBlock x:Name="{Binding Path=Person.LastName, ElementName=This}"/>
    </Grid>
</UserControl>

现在,每当设置Person属性时,您的控件将使用与该Person相关联的名字和姓氏更新自身。

2
如果这个UserControl最终会成为一个ContentControl,那么最好不要更改DataContext。一个简单的解决方案是给用户控件命名,并通过ElementName在绑定中引用它。 - user7116
sixlettervariables是正确的。查看此解释以了解原因。 - LPL
我已经按照六字母变量在我的原始帖子中建议的更改进行了修改。 - Curtis

2
您想称之为依赖属性,您可以从XAML绑定到它。
1-创建字段。
public static readonly DependencyProperty FirstNameProperty = 
    DependencyProperty.Register(
    "FirstName", typeof(Strin),

创建属性
public String FirstName
{
    get { return (String)GetValue(FirstNameProperty); }
    set { SetValue(FirstNameProperty, value); }
}

3- 你可以在XAML中使用它进行绑定,或者直接使用它。

<local:YourControlName FirstName="john"/>

<local:YourControlName FirstName="{Binding MyFirstName}"/>
  • 使用Resharper可以帮助你编写干净的代码,并拥有强大的智能提示功能。

谢谢,看起来就是我缺少的东西。我会努力把它整合在一起。 - Steven Deam
还有一个问题,我能否绑定整个控件而不仅仅是单个属性? - Steven Deam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接