2015-09-06 96 views
1

即時新手與wpf + mvvm,有一個簡單的mui:ModernTab控制項目harcoded。綁定mui ModernTab wpf mvvm

<mui:ModernTab Layout="List" SelectedSource="/Pages/Settings/Appearance.xaml"> 
     <mui:ModernTab.Links> 
      <mui:Link DisplayName="appearance" Source="/Pages/Settings/Appearance.xaml" /> 
      <mui:Link DisplayName="about" Source="/Pages/Settings/About.xaml" /> 
     </mui:ModernTab.Links> 
    </mui:ModernTab> 

我想對視圖模型是這樣的構造函數的dbdata在XAML代碼填充它標籤:

<ScrollViewer> 
     <mui:ModernTab Layout="List" Links="{Binding AllowedViews}" /> 
</ScrollViewer> 

在視圖模型C#構造函數:

public class ApplicationViewModel:ViewModelBase 
{ 
    private LinkCollection allowedViews; 

    public LinkCollection AllowedViews 
    { 
     get { return allowedViews; } 
     set { 
       allowedViews = value; 
       NotifyPropertyChanged("tabitem"); 
     } 
    } 

    public ApplicationViewModel() 
    { 
     allowedViews.Add(new Link() { DisplayName = "item1"}); 
     allowedViews.Add(new Link() { DisplayName = "item2" }); 
     allowedViews.Add(new Link() { DisplayName = "item3" }); 

    } 

    //allowedViews.Add(new Link() { DisplayName = "Otra Ventana", Source = new Uri("/Views/ModernWindow1.xaml", UriKind.RelativeOrAbsolute) }); 
} 

問題:

  1. 1-better better use a LinkCo選擇或列表來填充數據。
  2. 正確的方法來做綁定是與道具鏈接 on xaml?
  3. 有人可以提供任何文檔或示例嗎?

非常感謝。請原諒我的英語。

回答

0
public LinkCollection AllowedViews 
{ 
    get { return allowedViews; } 
    set { 
      allowedViews = value; 
      NotifyPropertyChanged("tabitem"); 
    } 
} 

這個「tabitem」應該是「AllowedViews」,對嗎?

0

這裏是動態鏈接的定義

<mui:ModernTab Layout="List" Links ="{Binding MyIEnumerable, Converter={StaticResource myCollectionToLinksConverter}}"> 
     <mui:ModernTab.ContentLoader> 
      <app:MyControlLoader /> 
     </mui:ModernTab.ContentLoader> 
</mui:ModernTab> 

隨後的交換器的定義添加到您的窗口或控件

<UserControl.Resources> 
    <MyCollectionToLinksConverter x:Key="myCollectionToLinksConverter"/> 
</UserControl.Resources> 

然後添加轉換器類

public class MyCollectionToLinksConverter: IValueConverter 
{ 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     var source = (ICollection<MyCollectionItem>)value; 
     return new LinkCollection(source.Select(i => new Link() {DisplayName = i.Name, Source = new Uri(v.i, UriKind.Relative)})); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

然後添加您的內容加載程序

class MyControlLoader: DefaultContentLoader 
{ 
    protected override object LoadContent(Uri uri) 
    { 
     var myTarget = UIModel.Instance.GetMyTargetObjectById(v => v.Name == uri.OriginalString); 

     return new YourTabContentControl() {DataContext = myTarget}; 
    } 
}