绑定mui ModernTab wpf mvvm

时间:2015-09-06 21:56:26

标签: wpf mvvm modern-ui data-binding

我是新手与wpf + mvvm,有一个简单的mui:ModernTab控件与项目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>

我想在viewModel的构造函数上使用dbdata填充it选项卡,在xaml代码上是这样的:

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

在viewModel c#constructor上:

 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 - 最好使用LinkCollection或List来填充数据。
  2. 进行绑定的正确方法是在xaml上使用prop 链接
  3. 有人可以提供任何文档或示例吗?
  4. 非常感谢。原谅我的英语。

2 个答案:

答案 0 :(得分:0)

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

这个&#34; tabitem&#34;应该是#34; AllowedViews&#34;,对吧?

答案 1 :(得分: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};
    }
}