Wpf 显示UserControl.Resources
在我的Xaml中,我将一个资源定义为vehicleDataInput,它提供一些文本框供用户输入数据。如果我没有为它定义一个x:Key,它就会出现在我的应用程序中,并正常工作。问题是,我想确定在我看来,它是在x:键的帮助下放置的,但我无法获得正确的绑定 您可以在我的代码中进一步看到,我尝试使用内容控件并绑定到“vehicleDataInput”,但在运行时它不起作用 我试图找到这个问题的答案,但似乎没有人能找到,这让我觉得我把这个问题搞错了,我错过了什么Wpf 显示UserControl.Resources,wpf,mvvm,datatemplate,Wpf,Mvvm,Datatemplate,在我的Xaml中,我将一个资源定义为vehicleDataInput,它提供一些文本框供用户输入数据。如果我没有为它定义一个x:Key,它就会出现在我的应用程序中,并正常工作。问题是,我想确定在我看来,它是在x:键的帮助下放置的,但我无法获得正确的绑定 您可以在我的代码中进一步看到,我尝试使用内容控件并绑定到“vehicleDataInput”,但在运行时它不起作用 我试图找到这个问题的答案,但似乎没有人能找到,这让我觉得我把这个问题搞错了,我错过了什么 <UserControl x:Cl
<UserControl x:Class="RegCarManager.Vehicles.CreateVehicle.CreateVehicleView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vehicles="clr-namespace:RegCarManager.Vehicles"
xmlns:vehiclesViewModel="clr-namespace:RegCarManager.Vehicles.CreateVehicle"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<DataTemplate x:Key="vehicleDataInput" DataType="{x:Type vehicles:VehicleModel}">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Top" >
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="1" Text="Model" VerticalAlignment="Center" Margin="5" />
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Model}" Margin="5" Width="150" />
<TextBlock Grid.Column="0" Grid.Row="2" Text="Reg number" VerticalAlignment="Center" Margin="5" />
<TextBox Grid.Column="1" Grid.Row="2" Text="{Binding RegNumber}" Margin="5" Width="150" />
</Grid>
</DataTemplate>
</UserControl.Resources>
<DockPanel Margin="20">
<ComboBox DockPanel.Dock="Top"
ItemsSource="{Binding Clients}"
DisplayMemberPath="FullName"
SelectedValuePath="ClientId"
SelectedValue="{Binding OwnerId, Mode=TwoWay}" />
<ContentControl Content="{DynamicResource vehicleDataInput}" />
<DockPanel DockPanel.Dock="Bottom">
<Button Content="Save" DockPanel.Dock="Right" Margin="10,2" VerticalAlignment="Center"
Command="{Binding Path=SaveNewVehicleCommand}" IsDefault="True" Width="100" />
</DockPanel>
<ContentControl Margin="20,10" Content="{Binding Path=NewVehicle}" />
</DockPanel>
模板用于生成显示内容的UI。这不是内容;它描述了内容应该如何呈现给用户
<ContentControl
Content="{Binding NewVehicle}"
ContentTemplate="{StaticResource vehicleDataInput}"
/>
我猜您的主视图模型中一定有一个VehicleModel
的实例。这就是NewVehicle
吗
在这种情况下,使用
StaticResource
还是DynamicResource
并不重要。不同之处在于,如果在运行时使用不同的模板替换资源字典中的vehicleDataInput
资源,DynamicResource
将注意这一点,并将更新ContentControl
。但是您不会这样做,所以StaticResource
可以 您的viewmodel是否具有类型为VehicleModel
的属性?您想在ContentControl中显示的VehicleModel
在哪里?是的,NewVehicle是我用来提供模型的属性,它与您一起工作的一段代码。我还要感谢您花时间解释一些关于静态和动态资源的问题。