【发布时间】:2010-11-29 08:09:24
【问题描述】:
您好,我想在 xaml 中执行以下操作:
我的控件类中有一个属性 FocusTarget,我想从当前类中分配一个 UIElement。这在 XAML 中可行吗?
<my:BaseControl x:Class="SectionControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
FocusTarget="myCtrl"> // this fails
..
<my:CodeBlockControl x:Name="myCtrl" />
..
</my:BaseControl>
更新: 我现在将该属性实现为依赖属性,但似乎没有发生分配,尽管我在 XAML 中分配了它。但是没有编译也没有运行时错误:
在xml中:
FocusTarget="{Binding ElementName=myCtrl}"
在cs中:
public static readonly DependencyProperty FocusTargetProperty;
static BaseControl()
{
FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata(null);
FocusTargetProperty = DependencyProperty.Register("FocusTarget", typeof(FrameworkElement), typeof(BaseControl), metadata, Validate);
}
public FrameworkElement FocusTarget
{
get { return GetValue(FocusTargetProperty)as FrameworkElement; }
set { SetValue(FocusTargetProperty, value); }
}
【问题讨论】:
-
您的“验证”回调会以某种方式停止分配吗?可能值得暂时评论一下,看看它是否开始工作。
-
不,这是一个除了总是返回 true 什么都不做的方法
-
当我尝试使用快捷方式制作一个完整的控件时,我遇到了属性未设置的问题;我的自定义控件是从 UIElement 派生的。教训:如手册所说,从 Control 派生!
标签: wpf xaml controls properties