1

im rookie with wpf + mvvm, have a simple mui:ModernTab control with items harcoded.

<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>

I want populate it tab with the dbdata on the constructor of viewModel something like this on xaml code:

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

on viewModel c# constructor as:

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

Questions:

  1. 1-is better use a LinkCollection or List to populate data.
  2. The right way to do the binding is with prop Links on xaml?
  3. someone can sahre any documentation or example?

Thanks a lot. excuse my english.

2 Answers2

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

This "tabitem" should be "AllowedViews", right?

Gellio Gao
  • 835
  • 6
  • 19
0

Here is a definition of dynamic links

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

then add a definition of a converter to your window or control

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

then add the converter class

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

then add your content Loader

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

        return new YourTabContentControl() {DataContext = myTarget};
    }
}
Yaugen Vlasau
  • 2,148
  • 1
  • 17
  • 38