简体   繁体   English

Silverlight / WPF中的Control和ContentControl有什么区别?

[英]What is the difference between Control and ContentControl in Silverlight/WPF?

Please explain the actual differences between Control and ContentControl because googling didn't yielded good results. 请解释Control和ContentControl之间的实际差异,因为谷歌搜索无法产生良好的结果。

Actually, I'm facing one issue related to this: I have a Autocompletebox control ( inheriting from ContentControl ). 实际上,我面临与此有关的一个问题:我有一个Autocompletebox控件继承自ContentControl )。 For a new value entered by user, the corresponding Property's Mode=TwoWay is working fine and Property's value is getting updated in ViewModel and same repeats if the user enters another new value. 对于用户输入的新值,相应的Property的Mode = TwoWay可以正常工作,并且在ViewModel中更新Property的值,如果用户输入另一个新值,则重复相同的操作。 But if a previously entered value is again entered by the user then the Property's value is not being updated. 但是,如果用户再次输入了先前输入的值,则不会更新该属性的值。

So I guess that may be this Autocompletebox control should inherit from Control class rather than ContentControl . 因此,我想这可能是Autocompletebox控件应该继承自Control类而不是ContentControl

Am I correct?, please add your inputs and feedbacks. 我正确吗?请添加您的输入和反馈。

EDIT - Adding pseudo code:: 编辑-添加伪代码::

Control class:- 控制等级:

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: XAML:

<MyControls:MyAutoBox Grid.Row="1"
                    Grid.Column="0"
                    Margin="10,0"
                    CanTypeIn="True"
                    MyProp="{Binding MyProp,  Converter={StaticResource NullToNumericConverter},Mode=TwoWay}"
<MyControls/>  

Thanks. 谢谢。

ContentControl is actually inherited from Control class. ContentControl实际上是从Control类继承的。 So the issue does not appear due to that. 因此,该问题不会因此而出现。 Property Changed mechanism will work, when a property value really changed. 当属性值确实发生更改时,“属性更改”机制将起作用。 If the new value and old value are equal, there is no need to update the ViewModel. 如果新值和旧值相等,则无需更新ViewModel。

Apart from that, Control class is the base class for most of the UI Elements in WPF. 除此之外,Control类是WPF中大多数UI元素的基类。 It holds the properties like Background, Foreground, Font, etc. 它包含背景,前景,字体等属性。

ContentControl is a class which represents an element, that can accept single item as child. ContentControl是一个代表元素的类,可以接受单个项目作为子元素。 For example, ListBoxItem, ComboBoxItem, etc are ContentControls. 例如,ListBoxItem,ComboBoxItem等是ContentControls。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM