问题描述
我相信我们能不能绑定到内部视图模型。
I believe we cannot bind to internal viewmodel.
因此我认为的IValueConverter,IMultiValueConverter,INotifyPropertyChanged的所有具有约束力的相关实施方案,INotifyCollectionChanged应该为了与XAML的工作总是公开的。
Therefore I think all binding related implementations of IValueConverter, IMultiValueConverter, INotifyPropertyChanged, INotifyCollectionChanged should be always public in order to work with XAML.
这是否正确?
更新:这是不是这样一个简单的问题,因为有多种可能的怪异的情况下,如嵌套视图模型类,或显式接口实现,或别的东西,我不知道其中有可能导致以不同的答案。正如我们已经可以看到WPF和SL4.0把内部的ViewModels不同的某些原因。
UPDATE: this isn't such an easy question because there are various possible weird cases, like nested viewmodel classes, or explicit interface implementations or something else that I am not aware of which could possibly lead to different answers. As we already can see WPF and SL4.0 treat internal viewmodels differently for some reason.
推荐答案
做了一些检查,在此基础上code:
Did some checking, based on this code:
<UserControl>
<UserControl.Resources>
<sa1:TestValueConverter x:Key="TestValueConverter" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Text="{Binding Data, Converter={StaticResource TestValueConverter}}" />
</Grid>
</UserControl>
public class TestViewModel : INotifyPropertyChanged
{
public string Data { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
public class TestValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return "-" + value.ToString() + "-";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
有趣的是,结果是WPF和Silverlight的不同
和的答案是:
在Silverlight的:
In Silverlight:
-
我相信我们能不能绑定到内部视图模型 - 是的,我们的不能绑定到内部类,我们也不能绑定到内部或专用性能,提供了绑定错误。
"I believe we cannot bind to internal viewmodel" - yes we cannotbind to internal class, nor can we bind to internal or privateproperties, gives binding error.
中的IValueConverter,IMultiValueConverter实现 - 是也应该是公共
"implementations of IValueConverter, IMultiValueConverter" - yes also should be public
INotifyPropertyChanged的,INotifyCollectionChanged - 这些接口是由视图模型类和一些集合,必将实现,因此这些类应该是公开的,因为我们已经知道了。和方法,实现接口不能是私有或内部明显的事件。
"INotifyPropertyChanged, INotifyCollectionChanged" - these interfaces are implemented by classes of ViewModel and some collection that will be bound, so these classes should be public as we already know. And methods and events that implement interfaces cannot be private or internal obviously.
在WPF:
-
我相信我们能不能绑定到内部视图模型 - 我们的可以绑定到内部类,但我们不能绑定到内部或私有财产
"I believe we cannot bind to internal viewmodel" - we can bind to internal class, but we cannot bind to internal or private properties.
的IValueConverter,IMultiValueConverter的实现 - 能是内部
"implementations of IValueConverter, IMultiValueConverter" - can be internal
INotifyPropertyChanged的,INotifyCollectionChanged - 见previous点
"INotifyPropertyChanged, INotifyCollectionChanged" - see previous points.
这篇关于仔细检查 - 它曾经是有意义的有内部视图模型类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!