2012-04-25 64 views
0

我完全不熟悉WPF。更改WPF中的選定元素

我正在MVVM模式中做一個簡單的應用程序。

我有一個在其中引用模型的viewmodel。該模型包含一些我想放在組合框中的netelements。

這裏是視圖模型的相關部分:

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); 
      } 
     } 
    } 
} 

我希望能夠在組合框中選擇一個NetElement並在activeElement設置它。

這裏是我目前的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> 

這不是一個組合框,但的TextBlocks的名單,但你可以看到它是怎麼回事。

如何從視圖設置ActiveElement?

回答

1

您ActiveElement屬性創建ComboBox的SelectedItem屬性綁定:

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

則視圖的DataContext屬性設置爲您的視圖模型。

+0

這太容易了。 – SoonDead 2012-04-25 21:18:58