2012-03-19 17 views
0

Items Itemsを取得する方法表示項目を選択しましたMVVMパターンを使用して項目テキストボックスの値を入力しますか?選択したアイテムの値を取得するには?

<ListBox Margin="0,25,0,0" Grid.Row="3" ItemsSource="{Binding Path=ViewModelSearchResults}" SelectedItem="{Binding Path=SelectedCategoryViewModel, Mode=TwoWay}"> 
        <ItemsControl.ItemTemplate> 
         <DataTemplate > 
          <Grid> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="*"/> 
            <ColumnDefinition/> 
           </Grid.ColumnDefinitions> 
           <TextBox Grid.Row="0" Grid.Column="0" Text="{Binding Path=CategoryName}" FontSize="14" FontWeight="Normal" /> 
           <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=CategoryID}" FontSize="14" FontWeight="Normal" Visibility="Hidden" /> 
          </Grid> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
       </ListBox> 

答えて

1

Viewsから何かを得るように:バインドしてください!はい、ItemsControlSelectedItemですが、ItemsControl自体には選択動作がありません。代わりにListBoxのようなものを使用してください。

+0

私はこのコードを使用してリストボックスを使用しました: SelectedItem = "{バインディングパス= SelectedCategoryViewModel、Mode = TwoWay}"バインディングは正しく動作せず、infinetループに行きます。 @Snowbear – Tulsi

+0

私はviewmodelでこのコードを使用しています。プライベートCategoryViewModel _CategoryViewModel; public CategoryViewModel SelectedCategoryViewModel { get {return _CategoryViewModel; } セット { if(_CategoryViewModel!= value) { SelectedCategoryViewModel = value; OnPropertyChanged( "SelectedCategoryViewModel"); } } } @Snowbear – Tulsi

+1

@Tulsiあなたのセッターでは、バックフィールドの代わりにセッターを使用しています。 'SelectedCategoryViewModel'を探してください – Snowbear

1

SelectedItemをプロパティにバインドし、そのプロパティを通じて必要な値にアクセスできます。

関連する問題