0

以下の最初のブロックでは、2番目のブロックと比較できるように、バインディングステートメントの1つに名前を付けるのを意図的に見逃しています。違いは、「ag2.item ...」行にないプロパティにあります。
アイテムは私のモデルです。

ブロック2では、ビューモデル(ag2.viewModel.itemViewModel)を指していることがわかります。

ビューモデルではなくクラスを指すようにするには、XAMLまたはコードビハインドで何をする必要がありますか?

ブロック1:

BindingExpressionパスエラー:「itemModel1」プロパティが「ag2.item、ag2、Version = 1.0.0.0、Culture = neutral、PublicKeyToken=null」に見つかりません。BindingExpression:Path ='itemModel1' DataItem ='ag2.item、ag2、Version = 1.0.0.0、Culture = neutral、PublicKeyToken = null'; ターゲット要素は'Windows.UI.Xaml.Controls.TextBlock'(Name ='null'); ターゲットプロパティは「テキスト」(タイプ「文字列」)です

ブロック2:

BindingExpressionパスエラー:「itemModel」プロパティが「ag2.viewModel.itemViewModel、ag2、Version = 1.0.0.0、Culture = neutral、PublicKeyToken=null」に見つかりません。BindingExpression:Path ='itemModel' DataItem ='ag2.viewModel.itemViewModel、ag2、Version = 1.0.0.0、Culture = neutral、PublicKeyToken = null'; ターゲット要素は'Windows.UI.Xaml.Controls.TextBlock'(Name ='null'); ターゲットプロパティは「ソース」(タイプ「文字列」)です

ブロック2の背後にあるコード:

    itemViewModel VM = new itemViewModel((Int32)navigationParameter);
    DataContext = VM;

また、ブロック1で、が設定されているGridViewへのバインドを行っていることにも注意してくださいItemSource="{Binding item}"

ブロック2では、グリッドとテキストブロックを使用してUIを構築しました。Text="{Binding Path=itemModel}"

更新:より良い理解を得るための努力で。私は自分のコードをそこに置いています:これがXAMLで、その下がViewModelで、その下が私のモデルです...私はMVVMを初めて使用するので、何が間違っているのか本当にわかりません。どんな助けでも大歓迎です。

XAML:

<common:LayoutAwarePage
    x:Name="pageRoot"
    x:Class="autoGarage2.VehicleItemDetailPage"
    IsTabStop="false"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:common="using:autoGarage2.Common"
    xmlns:local="using:autoGarage2"
    xmlns:data="using:autoGarage2"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">


    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid Style="{StaticResource LayoutRootStyle}">

        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="2*"/>
        </Grid.RowDefinitions>

        <!-- Back button and page title -->
        <Grid
         Style="{StaticResource LayoutRootStyle}" Grid.Row="0">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
            <TextBlock x:Name="pageTitle" Text="{StaticResource AppName}" Style="{StaticResource PageHeaderTextStyle}" Grid.Column="1"/>
        </Grid>



        <Grid Grid.Row="1">


            <Grid.ColumnDefinitions>
               <ColumnDefinition Width="Auto"/> 
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="3*"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>


                <StackPanel Grid.Row="0" Orientation="Horizontal" Width="auto" Margin="50,0,0,0" VerticalAlignment="Top"  >
                    <Grid HorizontalAlignment="Left" Width="250" Height="250">
                        <Border Background="White" BorderBrush="CornflowerBlue" BorderThickness="1">
                                <Image Source="{Binding Image}" Margin="50"/>
                            </Border>
                            <StackPanel VerticalAlignment="Bottom" Background="CornflowerBlue">
                                <StackPanel Orientation="Horizontal" DataContext="{Binding vehicles}">
                                <TextBlock Text="{Binding VehicleMake}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource PageSubheaderTextStyle}" Margin="5"/>
                                <TextBlock Text="{Binding VehicleModel}" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource PageSubheaderTextStyle}" Margin="5"/>
                                </StackPanel>
                            </StackPanel>
                        </Grid>
                </StackPanel>
                    <StackPanel Grid.Row="0" Grid.Column="1">
                        <Grid Margin="20,0,0,20">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="1*"/>
                                <RowDefinition Height="1*"/>
                                <RowDefinition Height="1*"/>
                                <RowDefinition Height="1*"/>                                
                                <RowDefinition Height="1*"/>                                
                                <RowDefinition Height="1*"/>
                                <RowDefinition Height="1*"/>
                                <RowDefinition Height="1*"/>
                                <RowDefinition Height="1*"/>                                
                                <RowDefinition Height="1*"/>    
                                <RowDefinition Height="1*"/> 
                                <RowDefinition Height="1*"/> 
                                <RowDefinition/>                                
                            </Grid.RowDefinitions>

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="1*"/>
                                <ColumnDefinition Width="1*"/>
                                <ColumnDefinition Width="2*"/>
                            </Grid.ColumnDefinitions>






                            <TextBlock Text="Vehicle Make:" Grid.Row="0" Grid.Column="0" FontSize="25" Foreground="Black" />
                            <TextBox Text="{Binding Path=VehicleMake}"  Grid.Row="0" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>

                            <TextBlock Text="Vehicle Model:" FontSize="25" Foreground="Black" Grid.Row="1" Grid.Column="0"/>
                            <TextBox Text="{Binding VehicleModel}"  Grid.Row="1" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>                            

                            <TextBlock Text="Vehicle Year:" FontSize="25" Foreground="Black" Grid.Row="2" Grid.Column="0"/>
                            <TextBox Text="{Binding VehicleYear}"  Grid.Row="2" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>

                            <TextBlock Text="License Plate:" FontSize="25" Foreground="Black" Grid.Row="3" Grid.Column="0"/>
                            <TextBox Text=""  Grid.Row="3" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/> 

                            <TextBlock Text="VIN #" FontSize="25" Foreground="Black" Grid.Row="4" Grid.Column="0"/>
                            <TextBox Text=""  Grid.Row="4" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1" />    

                            <TextBlock Text=" Current Mi/Km" FontSize="25" Foreground="Black" Grid.Row="5" Grid.Column="0"/>
                            <TextBox Text=""  Grid.Row="5" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>              

                            <TextBlock Text="" FontSize="25" Foreground="Black" Grid.Row="6" Grid.ColumnSpan="2"/>

                            <TextBlock Text="Last Oil Change" FontSize="25" Foreground="Black" Grid.Row="7" Grid.Column="0"/>
                            <TextBox Text=""  Grid.Row="7" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>              

                            <TextBlock Text="Last Oil Change Mi/Km" FontSize="25" Foreground="Black" Grid.Row="8" Grid.Column="0"/>
                            <TextBox Text=""  Grid.Row="8" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>

                            <TextBlock Text="" FontSize="25" Foreground="Black" Grid.Row="9" Grid.ColumnSpan="2"/>

                            <TextBlock Text="Reminder Mi/Km" FontSize="25" Foreground="Black" Grid.Row="10" Grid.Column="0"/>
                            <TextBox Text=""  Grid.Row="10" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>     

                            <TextBlock Text="Reminder Month(s)" FontSize="25" Foreground="Black" Grid.Row="11" Grid.Column="0"/>
                            <TextBox Text=""  Grid.Row="11" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>                          

                        </Grid>                     
                </StackPanel>

            </Grid>

    </Grid>
</common:LayoutAwarePage>

モデルの表示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Windows.Foundation.Collections;
using System.IO;


namespace autoGarage2.viewModel
{
    class vehicleViewModel
    {
        private IList<vehicle> m_vehicles;
        private IList<vehicle> m_vehicleItem;

        public IList<vehicle> vehicles
        {
            get { return m_vehicles; }
            set { m_vehicles = value; }
        }

        public IList<vehicle> vehicleItem
        {
            get { return m_vehicleItem; }
            set { m_vehicleItem = value; }
        }

        private IList<vehicle> getVehicleDetail(Int32 vId)
        {
            var vehicleItem =
                from v in vehicles
                where v.VehicleId == vId
                select v;
            if (vId > 0)
            {
                //vehicles.Clear();
                m_vehicles = new List<vehicle>();
                foreach (var item in vehicleItem)
                {
                    m_vehicles = new List<vehicle>
                    {
                        new vehicle(item.VehicleId, item.VehicleMake.ToString(), item.VehicleModel.ToString(), item.VehicleYear, item.Image.ToString())
                    };
                    //vehicle myVehicle = new vehicle(item.VehicleId, item.VehicleMake.ToString(), item.VehicleModel.ToString(), item.VehicleYear, item.Image.ToString());
                    //m_vehicles.Add(myVehicle);
                }

            }

            return m_vehicles;
        }

        public vehicleViewModel(Int32 vId)
        {
                m_vehicles = new List<vehicle>
                {        
                new vehicle(1, "Mazda", "3", 2011, "Assets/car2.png"),
                new vehicle(2, "Chevy", "Tahoe", 2004, "Assets/jeep1.png"),
                new vehicle(3, "Honda", "Goldwing", 2007 ,"Assets/moto1.png")
                };

                if (vId > 0)
                {
                    //m_vehicles = new List<vehicle>();
                    //m_vehicles = 
                    //getVehicleDetail(vId);
                    m_vehicles = new List<vehicle>
                    {        
                    new vehicle(2, "Chevy", "Tahoe", 2004, "Assets/jeep1.png"),
                    };

                }
        }



        #region dbCode

        //string dbName = "vehicle.db";


        //var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, dbName);    
        //using (var db = new SQLite.SQLiteConnection(dbPath))    
        //    {
        //        var list = db.Table<vehicle>().ToList();
        //        m_vehicles = new List<vehicle>();
        //        for (Int32 i = 0; i < list.Count; i++)
        //        {
        //            //m_vehicles.Add(db.Table<vehicle>().ToList());
        //        }
        //    }




        //foreach (vehicle item in m_vehicles)
        //{
        //    AllItems.Add(item);
        //}      
        #endregion

    }
}

モデル:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using SQLite;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace autoGarage2
{
    class vehicle : autoGarage2.Common.BindableBase
    {
        public vehicle()
        { 
        }

        public vehicle(string imagePath)
        {
            this._imagePath = imagePath;
        }
        public vehicle(Int32 vId, string vMake, string vModel, Int16 vYear, string imagePath)
        {
            this.m_vehicleID = vId;
            this.m_vehicleMake = vMake;
            this.m_vehicleModel = vModel;
            this.m_vehicleYear = vYear;
            this.m_vehicleName = vMake + " " + vModel;
            this._imagePath = imagePath;
        }


        private Int32 m_vehicleID;

        private String m_vehicleMake;
        private String m_vehicleModel;
        private Int16 m_vehicleYear;
        private string m_vehicleName;


        private ImageSource _image = null;
        private String _imagePath = null;
        private static Uri _baseUri = new Uri("ms-appx:///");

        //[AutoIncrement, PrimaryKey]
        public Int32 VehicleId
        {
            get
            {
                return m_vehicleID;
            }
            set
            {
                m_vehicleID = value;
                OnPropertyChanged("VehicleId");
            }
        }

        public String VehicleMake
        {
            get
            {
                return m_vehicleMake;
            }
            set
            {
                m_vehicleMake = value;
                OnPropertyChanged("VehicleMake");
            }
        }

        public String VehicleModel
        {
            get
            {
                return m_vehicleModel;
            }
            set
            {
                m_vehicleModel = value;
                OnPropertyChanged("VehicleModel");
            }
        }
    public Int16 VehicleYear
    {
        get
        {
            return m_vehicleYear;
        }
        set
        {
            m_vehicleYear = value;
            OnPropertyChanged("VehicleYear");
        }
    }

    public string VehicleName
    {
        get
        {
            return m_vehicleName;
        }
        set
        {
            m_vehicleName = value;
            OnPropertyChanged("VehicleName");
        }
    }

    public ImageSource Image
    {
        get
        {
            if (this._image == null && this._imagePath != null)
            {
                this._image = new BitmapImage(new Uri(vehicle._baseUri, this._imagePath));
            }
            return this._image;
        }

        set
        {
            this._imagePath = null;
            this.SetProperty(ref this._image, value);
        }
    }

    public void SetImage(String path)
    {
        this._image = null;
        this._imagePath = path;
        this.OnPropertyChanged("Image");
    }
}

}

4

3 に答える 3

1

TextBlockViewModel(DataContextウィンドウ/コントロールの)ではなく、別のオブジェクトにデータバインドしようとしているようです。それが正しい場合は、DataBindを実行するオブジェクト DataContextTextBlockまたは親を設定する必要があります。Grid

DataContext、コントロールのDataBind toへのパスを決定する際に使用され、ビジュアルツリーを継承します。したがって、DataContextがMyViewModelに設定されていて、を使用する場合Text="{Binding Path=itemModel}"、バインディングパスはになりますMyViewModel.itemModel

バインディングパスに含めたくない場合は、問題のコントロールまたは含まれているコントロールのMyViewModelを変更する必要があります。DataContextMVVMの場合、これは多くの場合、ViewModelのプロパティとして他のオブジェクトを公開することによって行われます。したがって、MyViewModelにプロパティItemModelがある場合、次のようになります。

public class ItemModel
{
    public string Property1 { get; }
    public string Property2 { get; }
}

public class MyViewModel
{
    public ItemModel ItemModel { get; private set; }
}

その場合、XAMLは次のようになります(MyViewModelが親ウィンドウ/コントロールのDataContextであると想定)。

<Grid Grid.Row="1" DataContext="{Binding ItemModel}">
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <TextBlock Text="{Binding property1}"/>
    <TextBlock Text="{Binding Property2}" Grid.Row="1"/>
</Grid>

また、2つのテキストボックスはItemModelオブジェクトのProperty1とProperty2にバインドされます。

于 2012-08-25T17:08:28.980 に答える
1

リストにバインドしようとしているようですが、XAMLでItemsControlを使用していません。おそらく、ListViewのようなものを使用し、そのItemsSourceを車両またはVehicleItemリストにバインドし、ItemTemplate / DataTemplateを使用してコレクション内の各アイテムの外観を定義し、コレクションが変更された場合はObservableCollectionを使用し、スワップした場合はINotifyPropertyChanged.PropertyChanged通知を発行する必要があります。それ以外の場合は、ItemsControlsのバインドに関する情報や、 AdamNathanのWPFUnleashedなどのXAMLに関する一般的な本を読むことをお勧めします。。次のステートメントでm_vehiclesを新しいものと交換するためだけに、m_vehiclesを設定しているようです。また、StackPanelのDataContextをリストに設定していますが、これは許可されていますが、機能しません。StackPanelの要素のDataContextはリスト全体であり、アイテムではないためです。 ItemsControlを使用します。

于 2012-08-26T04:23:41.643 に答える
0

週末にデータを見なかった後、データを表示することができました。基本的に、autoGarage2.vehiclesリストにプロパティが見つからないというバインディングエラーが発生していることに気付きました。だからニヤリと私はこのようにバインディングの前に置きました:

{Binding vehicles[0].vehicleModel}

次に実行したとき、データはそこにありました。もう少し考えた後、私は車両の単一のオブジェクトを作成することにしました。リストオブジェクトを作成する代わりに、単一の車両のプロパティのみを入力します。今、私はこのようなことをしています:

{Binding vehicleSingle.vehicleModel}

みんな助けてくれてありがとう。これは、MVVMがXAMLでどのように機能するかの微妙な違いだと思います...

于 2012-08-28T11:56:02.953 に答える