【问题标题】:Binding issue on CustomControlCustomControl 上的绑定问题
【发布时间】:2012-03-26 04:06:07
【问题描述】:

我在 WPF 上编写了一个 CustomControl,它只是一个带有 ContentPresenter 和一个按钮的 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”进行绑定的源。绑定表达式:路径=IsChecked;数据项=空;目标元素是'MyTestControl'(名称='');目标属性是“IsNextButtonEnabled”(类型“布尔”)

我不明白我的错误。我是不是做错了什么?

提前致谢。

【问题讨论】:

  • 您是否尝试过更改 Xaml 以使复选框具有绑定?意思是,从您的 TestControl 声明中删除 IsNextButtonEnabled=,并添加到 CheckBox,IsChecked="{Binding IsNextButtonEnabled, Mode=TwoWay}"。然后确保 CheckBox 的 DataContext 设置为 TestControl。

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


【解决方案1】:

试试这个,将复选框的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); }
  }
}

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    相关资源
    最近更新 更多