2012-04-25 13 views
0

私はWPFを初めて使っています。WPFで選択された要素を変更する

私はMVVMパターンで単純なアプリケーションを作っています。

モデルが参照されているビューモデルがあります。このモデルには、コンボボックスに入れたいネット要素が含まれています。私はコンボボックスでNetElementを選択し、それにActiveElementを設定できるようにしたいと思い

public class MainWindowVM : ViewModelBase 
{ 
    private Model _model = null; 

    public Model Model 
    { 
     get 
     { 
      return _model; 
     } 
    } 

    #region ActiveElement 

    private NetElement _activeElement = null; 

    public NetElement ActiveElement 
    { 
     get 
     { 
      return _activeElement; 
     } 
     set 
     { 
      if (_activeElement != value) 
      { 
       _activeElement = value; 
       RaisePropertyChanged("ActiveElement"); 
       if (ActiveElementChanged != null) 
        ActiveElementChanged(this, EventArgs.Empty); 
      } 
     } 
    } 
} 

は、ここでのviewmodelの関連する部分です。ここ

は私の現在のXAMLの関連する部分である:

<ItemsControl Background="White" IsTabStop="True" ItemsSource="{Binding Path=Model.RootNet.Elements}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Margin="2,6"> 
      <Hyperlink Command="{Binding Path=I'm not able to figure out what to write here}"> 
       <TextBlock Text="{Binding Path=Name}" /> 
      </Hyperlink> 
      </TextBlock> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

これは、コンボボックスが、テキストブロックのリストではありませんが、それが起こっている場所を確認できます。

ビューからActiveElementを設定するにはどうすればよいですか?

答えて

1

あなたActiveElementプロパティにコンボボックスのSelectedItemプロパティのバインディングを作成します。

<ComboBox SelectedItem="{Binding Path=ActiveElement}" ... /> 

は、あなたのビューモデルとビューのDataContextプロパティを設定します。

+0

あまりにも簡単です。 – SoonDead

関連する問題