繁体   English   中英

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

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

请解释Control和ContentControl之间的实际差异,因为谷歌搜索无法产生良好的结果。

实际上,我面临与此有关的一个问题:我有一个Autocompletebox控件继承自ContentControl )。 对于用户输入的新值,相应的Property的Mode = TwoWay可以正常工作,并且在ViewModel中更新Property的值,如果用户输入另一个新值,则重复相同的操作。 但是,如果用户再次输入了先前输入的值,则不会更新该属性的值。

因此,我想这可能是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));

}

视图模型: -

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/>  

谢谢。

ContentControl实际上是从Control类继承的。 因此,该问题不会因此而出现。 当属性值确实发生更改时,“属性更改”机制将起作用。 如果新值和旧值相等,则无需更新ViewModel。

除此之外,Control类是WPF中大多数UI元素的基类。 它包含背景,前景,字体等属性。

ContentControl是一个代表元素的类,可以接受单个项目作为子元素。 例如,ListBoxItem,ComboBoxItem等是ContentControls。

暂无
暂无

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

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