簡體   English   中英

綁定到自定義控件的依賴項屬性

[英]Binding To a Custom Control's Dependency Property

快速概述

我創建了一個自定義控件“ InputValuePage”。 我已經使用名為InputTitle的控件注冊了依賴項屬性。 在用戶控件InputTitle中綁定到文本塊效果很好,但是當我將此自定義控件作為框架或窗口中的子級使用時,我不能使用綁定來設置依賴項屬性。 以下是用於InputTitle屬性的代碼。

后面的自定義控件類代碼:

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 您的班級繼承自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一起使用。

顯然,它不適用於Page。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM