我已經在WPF上編寫了一個CustomControl,它只是一個帶有ContentPresenter和Button的ContentControl。CustomControl上的綁定問題
這裏是控制模板(位於主題/ 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本身的Content內部的另一個控件(例如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。所以,我沒有這一點,但它不工作:在執行,在輸出
<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>
:
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沒有改變)窗口我得到這個錯誤信息:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=cb'. BindingExpression:Path=IsChecked; DataItem=null; target element is 'MyTestControl' (Name=''); target property is 'IsNextButtonEnabled' (type 'Boolean')
我不明白我的錯誤。我做錯了什麼?
在此先感謝。
您是否嘗試過改變你的XAML中,這樣的複選框有捆綁?意思是,從你的TestControl聲明中刪除IsNextButtonEnabled =,並添加到CheckBox,IsChecked =「{Binding IsNextButtonEnabled,Mode = TwoWay}」。然後確保CheckBox的DataContext被設置爲TestControl。 – 2012-03-08 18:22:03