即使是最基本的例子,我也会疯狂地试图让它工作.我不能为我的生活得到约束力.这是一个不适合我的超级简单示例.我必须做错事.
我的控件库程序集中的自定义控件:
public class TestControl : Control
{
public static readonly DependencyProperty TestPropProperty =
DependencyProperty.Register("TestProp", typeof(string), typeof(TestControl), new UIPropertyMetadata(null));
public string TestProp
{
get { return (string)GetValue(TestPropProperty); }
set { SetValue(TestPropProperty, value); }
}
static TestControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TestControl), new FrameworkPropertyMetadata(typeof(TestControl)));
}
}
Run Code Online (Sandbox Code Playgroud)
它的XAML模板:
<Style TargetType="{x:Type local:TestControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:TestControl}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel>
<TextBlock Text="Testing..." />
<Label Content="{Binding TestProp}" Padding="10" />
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
这是XAML在wpf窗口中使用控件来引用我的控件库:
<Grid>
<ItemsControl Name="mylist">
<ItemsControl.ItemTemplate>
<DataTemplate> …Run Code Online (Sandbox Code Playgroud) 我有一个自定义文本框定义如下:
public class CustomTextBox : TextBox
{
public static DependencyProperty CustomTextProperty =
DependencyProperty.Register("CustomText", typeof(string),
typeof(CustomTextBox));
static CustomTextBox()
{
TextProperty.OverrideMetadata(typeof(SMSTextBox),
new FrameworkPropertyMetadata(string.Empty,
FrameworkPropertyMetadataOptions.Journal |
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
new PropertyChangedCallback(CustomTextBox_OnTextPropertyChanged));
}
public string CustomText
{
get { return (string)GetValue(CustomTextProperty); }
set { SetValue(CustomTextProperty, value); }
}
private static void CustomTextBox_OnTextPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
CustomTextBox customTextBox = d as CustomTextBox;
customTextBox.SetValue(CustomTextProperty, e.NewValue);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在绑定XAML中的自定义文本属性 -
<local:CustomTextBox CustomText="{Binding ViewModelProperty}" />
Run Code Online (Sandbox Code Playgroud)
我面临的问题是,当我在CustomTextBox中输入任何内容时,更改不会反映在ViewModelProperty中,即ViewModelProperty没有得到更新.CustomTextProperty正在更新,但我想我需要做一些额外的事情来使绑定工作.
我不做什么?我将不胜感激任何帮助.
谢谢