CustomControl上的绑定问题

时间:2012-03-08 16:59:32

标签: wpf xaml binding custom-controls dependency-properties

我在WPF上编写了一个CustomControl,它只是一个带有ContentPresenter和Button的ContentControl。

这是控件模板(位于Themes / Generic.xaml中):

<ControlTemplate TargetType="{x:Type local:TestControl}">
    <StackPanel Background="{TemplateBinding Background}">
        <ContentPresenter Content="{TemplateBinding Content}" />
        <Button Content="Next" IsEnabled="{TemplateBinding IsNextButtonEnabled}" />
    </StackPanel>
</ControlTemplate>

这是控制代码(TestControl.cs):

public class TestControl : ContentControl
{
    /// <summary>
    /// Identifies the IsNextButtonEnabled dependency property
    /// </summary>
    public readonly static DependencyProperty IsNextButtonEnabledProperty = DependencyProperty.Register(
        "IsNextButtonEnabled", typeof(bool), typeof(TestControl),
        new PropertyMetadata(true)
    );

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

    /// <summary>
    /// Gets or sets the value indicating if the Next button of the control is enabled
    /// </summary>
    [BindableAttribute(true)]
    public bool IsNextButtonEnabled
    {
        get
        {
            return (bool)GetValue(IsNextButtonEnabledProperty);
        }
        set
        {
            SetValue(IsNextButtonEnabledProperty, value);
        }
    }
}

我想将TestControl的IsNextButtonEnabled的值绑定到TestControl本身内容中的另一个控件(例如CheckBox)。

所以我这样做了并且它有效(当我选中或取消选中CheckBox时,按钮会正确启用或禁用它自己):

MainWindow.xaml

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication2"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <local:TestControl IsNextButtonEnabled="{Binding ElementName=cb, Path=IsChecked}">
            <CheckBox Name="cb" IsChecked="True" />
        </local:TestControl>
    </Grid>
</Window>

但我想要做的是在一个单独的xaml文件中声明我的TestControl。所以我做了这个,但它不起作用:

MainWindow.xaml

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication2"
    Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <local:MyTestControl />
    </StackPanel>
</Window>

MyTestControl.xaml(MyTestControl.cs未更改):

<local:TestControl x:Class="WpfApplication2.MyTestControl"
                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                   xmlns:local="clr-namespace:WpfApplication2"
                   IsNextButtonEnabled="{Binding ElementName=cb, Path=IsChecked}">
    <Grid>
        <CheckBox Name="cb" IsChecked="True" />
    </Grid>
</local:TestControl>

执行时,在“输出”窗口中出现此错误消息:

  

System.Windows.Data错误:4:找不到引用'ElementName = cb'的绑定源。 BindingExpression:路径=器isChecked;的DataItem = NULL; target元素是'MyTestControl'(Name =''); target属性是'IsNextButtonEnabled'(类型'Boolean')

我不明白我的错误。我做错了吗?

提前致谢。

1 个答案:

答案 0 :(得分:2)

尝试此操作,将复选框的IsChecked绑定到IsNextButtonEnabled属性

<ContentControl x:Class="WpfStackOverflowSpielWiese.TestControl"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                x:Name="control">

  <Grid>
    <CheckBox Name="cb"
              IsChecked="{Binding ElementName=control, Path=IsNextButtonEnabled}" />
  </Grid>
</ContentControl>

背后的代码

public partial class TestControl : ContentControl
{
  public TestControl() {
    this.InitializeComponent();
  }

  /// <summary>
  /// Identifies the IsNextButtonEnabled dependency property
  /// </summary>
  public static readonly DependencyProperty IsNextButtonEnabledProperty = DependencyProperty.Register(
    "IsNextButtonEnabled", typeof(bool), typeof(TestControl), new PropertyMetadata(true));

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

  /// <summary>
  /// Gets or sets the value indicating if the Next button of the control is enabled
  /// </summary>
  [Bindable(true)]
  public bool IsNextButtonEnabled {
    get { return (bool)this.GetValue(IsNextButtonEnabledProperty); }
    set { this.SetValue(IsNextButtonEnabledProperty, value); }
  }
}

希望这会有所帮助