gpt4 book ai didi

wpf - 呈现 UserControl.Resources

转载 作者:行者123 更新时间:2023-12-03 10:33:28 27 4
gpt4 key购买 nike

在我的 Xaml 中,我定义了一个资源作为 vehicleDataInput,它提供一些文本框供用户输入数据。如果我没有为它定义一个 x:Key ,它将在我的应用程序中出现并按其应有的方式工作。问题是我想决定在我看来它是在 x:Key 的帮助下放置的,但我无法正确绑定(bind)。

您可以在下面的代码中看到,我尝试使用内容控件并绑定(bind)到“vehicleDataInput”,但它在运行时不起作用。

我试图找到这个问题的答案,但似乎没有人知道,这让我觉得我以错误的方式解决这个问题,我错过了什么?

<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很好。

关于wpf - 呈现 UserControl.Resources,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44722439/

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