2017-06-23 5 views
1

私のXamlでは、データを入力するためのテキストボックスを表示するvehicleDataInputというリソースを定義しました。私はx:Keyを定義していないと、それは自分のアプリに自分自身を提示し、必要なときに動作します。問題は私がx:Keyの助けを借りて配置されていると私の見解で決定したいと思っていますが、私は正しいバインディングを得ることができません。現在のUserControl.Resources

私のコードでは、コンテンツコントロールと「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> 

+0

あなたのviewmodelは 'VehicleModel'型のプロパティを持っていますか?あなたのContentControlに表示したい 'VehicleModel'はどこにありますか? –

答えて

1

テンプレートは、コンテンツを表示するUIを生成するために使用されます。内容ではありません。これは、コンテンツをユーザーに提示する方法の説明です。

<ContentControl 
    Content="{Binding NewVehicle}" 
    ContentTemplate="{StaticResource vehicleDataInput}" 
    /> 

私はあなたのメインのviewmodelでVehicleModelどこかのインスタンスを持っていなければならないことを推測しています。それはNewVehicleなのですか?

この場合、StaticResourceまたはDynamicResourceのどちらを使用するかは問題になりません。違いは、実行時にリソース辞書のvehicleDataInputリソースを別のテンプレートに置き換えた場合、DynamicResourceがそれを監視してContentControlを更新するということだけです。しかし、あなたはそれをやっていないので、StaticResourceは大丈夫です。

+0

はいNewVehicleは、私がモデルを提供するために使用したプロパティでした。また、静的で動的なリソースを説明する時間をとっていただき、ありがとうと思います。 –