[英]WPF How to use my Resource Dictionary containing a data template within my main XAML page
我的资源字典,其中包含我希望用于与其关联的每个 VM 的数据模板。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModel="clr-namespace:IntSimulator.ViewModels.EquipmentVMs"
xmlns:converters="clr-namespace:IntSimulator.Converters"
xmlns:local="clr-namespace:IntSimulator.ResourceDictionary">
<converters:ScaleXCoordConverter x:Key="ScaleXCoordConverter"></converters:ScaleXCoordConverter>
<converters:ScaleYCoordConverter x:Key="ScaleYCoordConverter"></converters:ScaleYCoordConverter>
<DataTemplate x:Key="KeyMcKeyface" DataType="{x:Type viewModel:CircuitTrackViewModel}">
<ItemsControl ItemsSource="{Binding LineCoords}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<!--When we click on the line, it will increase Z-index and change colour, to bring occupied track forward and highlight -->
<Line
X1="{Binding X1, Converter={StaticResource ScaleXCoordConverter}}" Y1="{Binding Y1, Converter={StaticResource ScaleYCoordConverter}}"
X2="{Binding X2, Converter={StaticResource ScaleXCoordConverter}}" Y2="{Binding Y2, Converter={StaticResource ScaleYCoordConverter}}"
Stroke="{Binding DataContext.Colour.Colour, RelativeSource={RelativeSource AncestorType=ContentPresenter, AncestorLevel=2}}"
StrokeThickness="1" StrokeEndLineCap="{Binding EndLineCap}" StrokeStartLineCap="{Binding StartLineCap}">
<Line.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding DataContext.OccupyTrackCommand, RelativeSource={RelativeSource AncestorType=ContentPresenter, AncestorLevel=2}}" CommandParameter="True"/>
</Line.InputBindings>
<Line.RenderTransform>
<TranslateTransform X="{Binding DataContext.DisplayViewModel.MainOffsetX, RelativeSource={RelativeSource AncestorType=Window, AncestorLevel=1}}"
Y="{Binding DataContext.DisplayViewModel.MainOffsetY, RelativeSource={RelativeSource AncestorType=Window, AncestorLevel=1}}"/>
</Line.RenderTransform>
</Line>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
我已经添加
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary x:Key="CircuitTracksResourceDictionary" Source="ResourceDictionary/CircuitTracksResourceDictionary.xaml"/>
</ResourceDictionary>
我的 App.xaml。 我现在希望在我的主 xaml 页面中使用此资源字典,但不确定如何执行此操作。
我曾经使用用户控件来执行此操作,如下所示:
//OLD CODE
<DataTemplate DataType="{x:Type viewModel:CircuitTrackViewModel}">
<userControlLines:CircuitTrackUserControl DataContext="{Binding}"></userControlLines:CircuitTrackUserControl>
</DataTemplate>
但这会导致性能问题。 理想情况下,我只想将数据模板放在我的主 xaml 页面中,但使用我的资源字典作为 static 资源。 先感谢您。
从DataTemplate
中删除x:Key
以使其隐含:
<DataTemplate DataType="{x:Type viewModel:CircuitTrackViewModel}">
然后,您只需要将ResourceDictionary
合并到您的App.xaml
中,即可将其应用于整个应用程序中的所有CircuitTrackViewModel
对象:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="..." />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.