2016-09-28 3 views
0

私はWPFを学び、その上に物事を行うMVVMスタイルを得ようとしています。ComboBoxオブジェクトタイプを印刷

私はコンボボックスにコードを表示したいという単純な練習アプリケーションを持っています。

マイコード

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace SteamCodes 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     private ObservableCollection<Codes> codes; 
     public MainWindow() 
     { 
      InitializeComponent(); 

      codes = new ObservableCollection<Codes>() 
      { 
       new Codes() {CodeID = "1", Code="CODETEXT"} 
      }; 
      steamCode.ItemsSource = codes.ToString(); 
     } 
    } 

    public class Codes 
    { 
     public string CodeID { get; set; } 
     public string Code { get; set; } 
    } 
} 

私のXAML

<Window x:Class="SteamCodes.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:SteamCodes" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <ComboBox x:Name="steamCode" ItemsSource="{Binding Source = Codes}" HorizontalAlignment="Left" Height="43" Margin="122,37,0,0" VerticalAlignment="Top" Width="259"/> 
    </Grid> 
</Window> 

現時点では私のコンボボックスは、コンボボックス内の各オプションとしてを通して引っ張っているライン「System.Collections.Objectmodelからの手紙です。 ObservableCollection`1 [SteamCodes.Codes] '

これらの文字のすべては、コンボボックスでは別のドロップダウンオプションです。

私が間違っているアイデア。

+2

'データ項目の収集。文字の集合である文字列を(バインディングではなく)割り当てています。 XAMLでのバインディング式は機能しません。基本については、[データバインディングの概要](https://msdn.microsoft.com/en-us/library/ms752347(v = vs.110).aspx)を参照してください。 – Clemens

+0

ありがとう、私は今そのリンクを見てみましょう。 –

+0

それ以外に何が必要ですか?コレクション全体を文字列にしています。あなたがコード化しなければならない画像、コンボボックスがアイテムのソースが2つの値に設定されている場合、どのように表示するか決定する必要があります - > CodeIDとコード –

答えて

4

あなたComboBoxItemSourceは、アイテムのコレクションではなく、文字列でなければなりません:

steamCode.ItemsSource = codes; 

あなたはまた、DisplayMemberPathプロパティを設定することで、コンボボックスに表示される値として考慮しなければならないアイテムのどのプロパティを指定する必要があります。

steamCode.DisplayMemberPath = "Code"; 

あなたがSelectedValuePathプロパティを使用する必要があり、実際の選択値として使用されますバインドされたオブジェクトのどのプロパティを指定するには:

steamCode.SelectedValuePath = "CodeID"; 

MVVMのアプローチはこれです:

public class ViewModel 
{ 
    public ObservableCollection<Codes> Codes { get; } 
     = new ObservableCollection<Codes>(); 
} 

メイン・ウィンドウコンストラクタ:

public MainWindow() 
{ 
    InitializeComponent(); 

    var viewModel = new ViewModel(); 
    viewModel.Codes.Add(new Codes { CodeID = "1", Code = "CODETEXT" }); 

    DataContext = viewModel; 
} 

XAML:ItemsSource`がバインドされなければならない

<ComboBox ItemsSource="{Binding Codes}" DisplayMemberPath="Code" .../> 
+0

それはすばらしく、XAMLのバインディングはどこにありますか?私はこれがMVVMのやり方だと思ったのですが?もし私がsteamCode.ItemsSource = codesを割り当てるなら、コード内にバインドする必要はありませんか?多くのお返事ありがとうございました! –

+0

はい、ItemsSourceをXAMLに設定する必要はありません。これは、これを行う方法の1つであるコードビハインドで既に行っているためです。このチュートリアルは、WPFでのデータバインディングがどのように機能するかについて、より良い考えを得るためにこの記事を見てください。http://www.codeproject.com/Articles/29054/WPF-Data-Binding-Part – Alex

+0

このチュートリアルも良いスタートです。http ://www.wpf-tutorial.com/data-binding/introduction – Alex