首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当前UserControl.Resources

当前UserControl.Resources
EN

Stack Overflow用户
提问于 2017-06-23 21:17:42
回答 1查看 473关注 0票数 1

在我的Xaml中,我定义了一个名为vehicleDataInput的资源,它提供了一些文本框供用户输入数据。如果我没有为它定义一个x:Key,它就会出现在我的应用程序中,并正常工作。问题是,我想决定在我的视图中,它是通过x:Key来放置的,但是我不能得到正确的绑定。

您可以在下面的代码中看到,我曾尝试使用content控件并绑定到"vehicleDataInput“,但它在运行时不起作用。

我试图找到这个问题的答案,但似乎没有人有答案,这让我认为我处理这个问题的方式是错误的,我错过了什么?

代码语言:javascript
复制
<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>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-23 21:25:58

模板用于生成显示内容的UI。它不是内容,而是内容应该如何呈现给用户的描述。

代码语言:javascript
复制
<ContentControl
    Content="{Binding NewVehicle}"
    ContentTemplate="{StaticResource vehicleDataInput}" 
    />

我猜您的主视图模型中一定有一个VehicleModel的实例。这就是NewVehicle吗?

在这种情况下,您使用的是StaticResource还是DynamicResource并不重要。不同之处在于,如果您在运行时将资源字典中的vehicleDataInput资源替换为不同的模板,DynamicResource将对此进行监视并更新ContentControl。但是您不会这样做,所以StaticResource很好。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44722439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档