【发布时间】:2014-12-04 13:48:26
【问题描述】:
请解释 Control 和 ContentControl 之间的实际区别,因为谷歌搜索没有产生好的结果。
实际上,我面临一个与此相关的问题: 我有一个 Autocompletebox 控件(继承自 ContentControl)。对于用户输入的新值,相应的属性的 Mode=TwoWay 工作正常,并且属性的值在 ViewModel 中得到更新,如果用户输入另一个新值,同样会重复。但如果用户再次输入之前输入的值,则不会更新属性的值。
所以我猜可能这个 Autocompletebox 控件 应该继承自 Control 类 而不是 ContentControl。
我说的对吗?请添加您的意见和反馈。
编辑 - 添加伪代码::
控制类:-
public class MyAutoBox : ContentControl
{
public int MyProp
{
get { return (int)GetValue(MyPropProperty); }
set { SetValue(MyPropProperty, value); }
}
public static readonly DependencyProperty MyPropProperty =
DependencyProperty.Register("MyProp", typeof(int), typeof(MyAutoBox), new PropertyMetadata(0));
}
ViewModel:-
public class MyViewModel : ViewModelBase, INavigationAware
{
private int MyProp;
public int MyProp
{
get { return MyProp; }
set
{
if (MyProp != value)
{
MyProp = value;
RaisePropertyChanged(() => MyProp);
}
}
}
}
Xaml:
<MyControls:MyAutoBox Grid.Row="1"
Grid.Column="0"
Margin="10,0"
CanTypeIn="True"
MyProp="{Binding MyProp, Converter={StaticResource NullToNumericConverter},Mode=TwoWay}"
<MyControls/>
谢谢。
【问题讨论】:
-
它不是 MS AutoCompleteBox。是我们开发的自定义控件,继承自ContentControl。
标签: c# .net wpf xaml silverlight