绑定到自定义控件的依赖项 属性

Binding To a Custom Control's Dependency Property

快速概览

我创建了一个自定义控件 "InputValuePage"。 我已经为这个名为 InputTitle 的控件注册了一个依赖项 属性。 在用户控件 InputTitle 中绑定到文本块效果很好,但是当我将此自定义控件用作框架中的 child 或 window 时,我无法使用绑定来设置我的依赖属性。下面是用于 InputTitle 属性.

的代码

自定义控件Class代码隐藏:

public partial class InputValuePage : Page
{
public static readonly DependencyProperty InputTitleProperty  =
    DependencyProperty.Register("InputTitle", typeof(string), typeof(InputValuePage));

public string InputTitle
    {
        get { return (string)GetValue(InputTitleProperty); }
        set { SetValue(InputTitleProperty, value); }
    }

public InputValuePage()
    {
        InitializeComponent();
    }
}

用法示例:

<Frame Grid.Row="2" 
           Grid.ColumnSpan="2"
           Grid.RowSpan="2"
           x:Name="DisFrame">
        <Frame.Content>
            <local:InputValuePage 
                                  InputMessage="This gets set in the control." 
                                  InputTitle="{Binding ElementName=DisFrame, Path=Name}" 
                                  HostWindow="{Binding ElementName=DemoWindow}">
            </local:InputValuePage>
        </Frame.Content>
    </Frame>

澄清一下XAML中设置的三个值都是依赖属性。当提供字符串时,可以成功设置输入消息和标题,但数据绑定实际上从未设置值。我缺少什么来允许绑定数据?

如果您制作自定义控件,则效果很好。自定义控件继承自 Control。您的 class 继承自 Page.

我已经用下面的代码试过了:

using System.Windows;
using System.Windows.Controls;

namespace WpfCustomControlLibrary1
{
  public class InputValuePage : Control
  {
    public static readonly DependencyProperty InputTitleProperty =
      DependencyProperty.Register ("InputTitle", typeof (string), typeof (InputValuePage));

    public string InputTitle
    {
      get { return (string)GetValue (InputTitleProperty); }
      set { SetValue (InputTitleProperty, value); }
    }

    static InputValuePage ( )
    {
      DefaultStyleKeyProperty.OverrideMetadata (typeof (InputValuePage), new FrameworkPropertyMetadata (typeof (InputValuePage)));
    }
  }
}

这是我的 Generic.xaml 文件中的 ControlTemplate。请注意,您可以使用 TemplateBinding 访问模板中的 属性。

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfCustomControlLibrary1">
  <Style TargetType="{x:Type local:InputValuePage}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:InputValuePage}">
          <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">

            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20" Text="{TemplateBinding InputTitle}"/>

          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>

Visual Studio在您创建自定义控件项目时自动生成一个ControlTemplate。

在我的测试项目中,我将 InputTitle 属性 连接到 TextBox 的内容。

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        xmlns:cc="clr-namespace:WpfCustomControlLibrary1;assembly=WpfCustomControlLibrary1"
        Title="MainWindow" Height="200" Width="200">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <TextBox Grid.Row="0" x:Name="TitleBox" Text="ABC"/>

    <Frame Grid.Row="1" x:Name="DisFrame">
      <Frame.Content>
        <cc:InputValuePage InputTitle="{Binding ElementName=TitleBox, Path=Text}"/>
      </Frame.Content>
    </Frame>

  </Grid>
</Window>

我很确定这也适用于 UserControl,如果您觉得这样更容易的话。

显然,它不适用于页面。