2012-07-14 33 views
9

Windows 8 Release Preview(最新のパッチがインストールされています)を使用して、WinRT/C#XAML Metroアプリで奇妙な問題が発生しました。私は、その値がItemsSourceSelectedValueのViewModelのプロパティにバインドされているComboBoxを、使用しています:背後ComboBox SelectedValueが表示されません

<ComboBox SelectedValue="{Binding MySelectedValue, Mode=TwoWay}" 
      ItemsSource="{Binding MyItemsSource, Mode=OneWay}" 
      Width="200" Height="30" /> 

コード:

public MainPage() 
{ 
    this.InitializeComponent(); 

    DataContext = new TestViewModel(); 
} 

や文字列を使用してTestViewModelの非常にシンプルな定義は、:

public class TestViewModel : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    private IEnumerable<string> _myItemsSource = new List<string> 
     { 
      "Test Item 1", 
      "Test Item 2", 
      "Test Item 3" 
     }; 
    public IEnumerable<string> MyItemsSource 
    { 
     get { return _myItemsSource; } 
    } 

    private string _mySelectedValue = "Test Item 2"; 
    public string MySelectedValue 
    { 
     get { return _mySelectedValue; } 
     set 
     { 
      _mySelectedValue = value; 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs("MySelectedValue")); 
      } 
     } 
    } 
} 

今、私はこの単純な解決策がうまくいくと思っていました...しかし、私がアプリを起動すると、SelectedValue="Test Item 2"が表示されない場合、ComboBoxは空のままです。ブレークポイントを設定することによって、ビューのDataContextを設定すると、バインドされた値MyItemsSourceMySelectedValueが、ビューモデルからcorectly取得されていることがわかりました。この操作の後、ComboBox.SelectedValueプロパティは実際には"Test Item 2"に設定されますが、表示されません。また、UI上のユーザーアクションによってComboBoxで選択した値を変更すると、変更された値がComboBoxに表示され、それに応じてView Modelプロパティが更新されることに気付きました。だから、MySelectedValue View Modelプロパティの最初の視覚化を除いて、すべてがうまくいくようです。私はそれについて本当に必死になっています...

これは最も簡単な例ですが、原点では、コンボボックス全体にバインドし、DisplayMemberPathSelectedValuePathと設定したいと考えていました。残念ながら、同じ問題が発生します。

+0

あなたのための仕事ですか? –

+0

'selectedValue = itemsSource [1]'のように、selectedValueに「新しい文字列」を割り当てるのではなく、コレクションから実際の要素を取得すると問題は解決しませんか? – Patrick

答えて

29

である私は私の例では、問題を発見した見つけることができますより前ItemsSourceプロパティ。このように両方の定義を入れ替えると、次のようになります。

<ComboBox ItemsSource="{Binding MyItemsSource, Mode=OneWay}" 
     SelectedValue="{Binding MySelectedValue, Mode=TwoWay}" 
     Width="200" Height="30" /> 

これは本当に奇妙で厄介です。今私は知りたいです:これはバグですか、設計ですか? XAMLで定義されたプロパティの順序にかかわらずコントロールが動作しているはずなので、これはバグだと思います。

+0

これはSilverlight 2以降です。 –

+0

チップをありがとう。私は何年もSilverlight/WPF/Windows8を使用してきたことがあります。最近、Windows8のアプリケーションを開発しているときにこの問題に遭遇しましたが、 'SelectedValue'が' ItemsSource'バインディングの前にあることに気づくのにしばらく時間がかかりました。 –

+0

本当ですか?私はこれが重要だとは思わなかった –

2

これはワーキングソリューションです:XAMLマークアップでは、私はSelectedValueプロパティを定義した:あなたがここにhttps://skydrive.live.com/?cid=b55690d11b67401d&resid=B55690D11B67401D!209&id=B55690D11B67401D!209

<ComboBox Width="300" Height="32" HorizontalAlignment="Left" DisplayMemberPath="Name" 
        VerticalAlignment="Top" ItemsSource="{Binding PersonCollection}" 
        SelectedItem="{Binding SelectedPerson, Mode=TwoWay}"></ComboBox> 

ViewModleクラスは

public class ViewModel:BaseViewModel 
    { 
     private Person selectedPerson; 
     public Person SelectedPerson { 
      get { return this.selectedPerson; } 
      set { this.selectedPerson = value; 
      this.RaisePropertyChanged("SelectedPerson"); 
      } 
     } 
     public ObservableCollection<Person> PersonCollection { get; set; } 

     public ViewModel() 
     { 
      this.PersonCollection = new ObservableCollection<Person>(); 
      this.PopulateCollection(); 

      //setting first item as default one 
      this.SelectedPerson = this.PersonCollection.FirstOrDefault(); 
     } 

     private void PopulateCollection() 
     { 
      this.PersonCollection.Add(new Person { Name="Oscar", Email="[email protected]" }); 
      this.PersonCollection.Add(new Person { Name = "Jay", Email = "[email protected]" }); 
      this.PersonCollection.Add(new Person { Name = "Viral", Email = "[email protected]" }); 
     } 
    }