gpt4 book ai didi

silverlight - Silverlight 中的类型化数据模板

转载 作者:行者123 更新时间:2023-12-03 10:40:41 26 4
gpt4 key购买 nike

我的理解是 Silverlight 不支持具有 DataType 属性的 DataTemplates。

那么你将如何在 SL 中完成以下任务(作者是 Josh Smith,完整链接如下)。简而言之,他的意思是,如果您将 TabControl 的选项卡页绑定(bind)到 ViewModel 集合,WPF 将通过查找具有适当(匹配)DataType 集的 DataTemplate 来找出如何动态显示每个选项卡。很酷,但我想知道您将如何(可以?)在 Silverlight 中执行此操作。

Applying a View to a ViewModel

MainWindowViewModel indirectly adds and removes Workspace­ViewModel objects to and from the main window's Tab­Control. By relying on data binding, the Content property of a TabItem receives a ViewModelBase-derived object to display. ViewModelBase is not a UI element, so it has no inherent support for rendering itself. By default, in WPF a non-visual object is rendered by displaying the results of a call to its ToString method in a TextBlock. That clearly is not what you need, unless your users have a burning desire to see the type name of our ViewModel classes!

You can easily tell WPF how to render a ViewModel object by using typed DataTemplates. A typed DataTemplate does not have an x:Key value assigned to it, but it does have its DataType property set to an instance of the Type class. If WPF tries to render one of your ViewModel objects, it will check to see if the resource system has a typed DataTemplate in scope whose DataType is the same as (or a base class of) the type of your ViewModel object. If it finds one, it uses that template to render the ViewModel object referenced by the tab item's Content property.

The MainWindowResources.xaml file has a Resource­Dictionary. That dictionary is added to the main window's resource hierarchy, which means that the resources it contains are in the window's resource scope. When a tab item's content is set to a ViewModel object, a typed DataTemplate from this dictionary supplies a view (that is, a user control) to render it, as shown in Figure 10.in Figure 10.



http://msdn.microsoft.com/en-us/magazine/dd419663.aspx在图 10 中。

最佳答案

这是您可以做到的一种方法。我过去曾使用过这样的技术,并取得了巨大的成功。

考虑一个非常简单的容器,它将像这样为您创建 View :

public class ViewMapper : ContentControl
{
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);

if (e.Property.Name == "DataContext")
WhenDataContextChanges();
}

private void WhenDataContextChanges()
{
if (DataContext == null)
Content = null;
else
Content = ViewFactory.GetView(DataContext.GetType());
}
}

编辑

因此,您可以使用此控件为您进行映射:
<Border DataContext="{Binding MyViewModel}">
<ViewMapper />
</Border>

结束编辑

请注意 ViewMapper只需等待数据上下文更改,查找数据类型的适当 View ,然后创建一个新 View 。它依赖于 ViewFactory,这是一个将类型映射到 View 的非常简单的静态查找:
public class ViewFactory
{
private static readonly Dictionary<string, Func<UIElement>> _registry = new Dictionary<string, Func<UIElement>>();

private static string Key(Type viewModelType)
{
return viewModelType.FullName;
}

public static void RegisterView(Type viewModelType, Func<UIElement> createView)
{
_registry.Add(Key(viewModelType), createView);
}

public static UIElement GetView(Type viewModelType)
{
var key = Key(viewModelType);
if (!_registry.ContainsKey(key))
return null;

return _registry[key]();
}
}

然后,您只需要在某个地方注册 View 映射:
ViewFactory.RegisterView(typeof(SomeViewModel), () => new SomeView());

注意 ViewFactory 可以很容易地使用 Activator.CreateInstance而不是使用 Func 机制。更进一步,您可以使用 IoC 容器...您始终可以决定通过 ViewModel 上的字符串 Name 属性而不是类型进行映射...这里的可能性是无穷无尽的。

关于silverlight - Silverlight 中的类型化数据模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4026973/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com